need Javascript help

489
Australia
Central Coast,
Hi all.

I'm in the process of learning JavaScript
I'm writing a javascript program for an assignment, and I can't seem to get my looping working correctly

I'm trying to terminate the loop if the phrase 'Quit' is entered in the name prompt (1st prompt)

I am then trying to get it to continue if either 'I', 'i', 'D' or 'd' are entered in the next prompt. If one of these characters is not entered, it is supposed to reset the loop, and return to the first prompt

I have consulted my class notes, the internet and other resources, but just cant seem to get it to work

all variables are declared, and all statments are ended correctly with a }

Code:
while (cont = true){
	name = prompt('Enter customer name (type \'Quit\' to cancel)', 'Name');
		if (name == 'quit' || name == 'Quit' || name == 'QUIT'){
			cont = false
		}
		else {		
			cont = true
			type = prompt('Enter the customer type: (Industrial [I] or Domestic [D]', 'D');
			if (type != 'I' || type != 'i' || type != 'D' || type != 'D'){
				invalid = invalid + 1
			}
			else{
				*other statements*
			}
}
 
I've never really played with Javascript but I have done a lot in Java. It might be easier to help if you describe the behavior of your code as it is now.

The main problem I see is that the statement below will always evaluate to true... even if one of the four correct inputs has been entered, therefore always incrementing the invalid counter:
Code:
if (type != 'I' || type != 'i' || type != 'D' || type != 'D'){


Maybe try something like this?

Code:
while (cont = true){
	name = prompt('Enter customer name (type \'Quit\' to cancel)', 'Name');
	
	if (name == 'quit' || name == 'Quit' || name == 'QUIT'){
		cont = false;
	}
	
	else {	
                
                type = prompt('Enter the customer type: (Industrial [I] or Domestic [D]', 'D');
                	
		if (type == 'I' || type == 'i'){
                                              
                       	//Do industrial stuff
							}
                else if(type == 'D' || type == 'D'){

                        //Do domestic stuff

		else{
			
                        invalid = invalid + 1;

		}
	}
}
 
Last edited:
Well the main problem I see is that you never close your "else" block. (Or you never close your "while" block, depending on how you look at it. Based on indenting though, it's the else block.)

Also, dch is correct about that one statement always evaluating to true. It's impossible for that statement not to return true. I think you want to change those "||" to "&&".
 
Back