Jump to content

while statement does not exit


Modify_inc

Recommended Posts

Can someoe please tell me what is wrong with this while statement? It will execute, but it continues even if the condition is false (which is suppose to exit the loop). I do not understand why it will not exit the loop. document.write("Welcome to Mack's Coffee Cove!"+ BR); while (cat != "morning" || cat != "noon" || cat != "evening") { cat = prompt("Enter the card category (morning, noon, evening): ",ES); } I've also tried it with 'do' at the begining and the 'while' statment at the end with same results. Is it something to do with my conditon? Mike

Link to comment
Share on other sites

now why does this one exit while the condition is still true? I even initialized the variable with a letter with the same results. I also tried swapping the operator just in case I had it reversed as I did previously, but it then loops continually whether I type a character or a number. while (cards == NaN) { cards = prompt("Enter the number of cards collected for the " + cat + " batch: ",ES);}

Link to comment
Share on other sites

NaN is not equal to NaN. That's just how it works.Use isNaN() to check if a value is NaN. http://www.w3schools...jsref_isnan.asp
k, got it thanks. Can you tell me why this line works, but then why does it put undefined before the answer though?It's like it's trying to concatenate the variable instead of just displaying the stored value for the variable(s).I say that because I initialized the m variable to 0, and it displayed Morning total: 015 (appending the zero to the total) if it helps, I am using m += cards to achieve this. Also tried m = m + cards with same results. document.write("Morning total: " + m + " Noon Total: " + n + " Evening Total: " + e + BR); Thanks againMike Edited by Modify_inc
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...