Jump to content
Sign in to follow this  
Modify_inc

while statement does not exit

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

Share this post


Link to post
Share on other sites

Mixing the != with the || operator like that means the condition will always return true. Try changing the || operators to && .

Share this post


Link to post
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);}

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...