Modify_inc Posted May 5, 2013 Share Posted May 5, 2013 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 More sharing options...
Don E Posted May 5, 2013 Share Posted May 5, 2013 For the while condition, try using && instead of || Link to comment Share on other sites More sharing options...
jeffman Posted May 5, 2013 Share Posted May 5, 2013 Mixing the != with the || operator like that means the condition will always return true. Try changing the || operators to && . Link to comment Share on other sites More sharing options...
Modify_inc Posted May 5, 2013 Author Share Posted May 5, 2013 that was it, thank you very much! Link to comment Share on other sites More sharing options...
Modify_inc Posted May 5, 2013 Author Share Posted May 5, 2013 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 More sharing options...
Ingolme Posted May 5, 2013 Share Posted May 5, 2013 NaN is not equal to NaN. That's just how it works.Use isNaN() to check if a value is NaN. http://www.w3schools.com/jsref/jsref_isnan.asp Link to comment Share on other sites More sharing options...
Modify_inc Posted May 6, 2013 Author Share Posted May 6, 2013 (edited) 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 May 6, 2013 by Modify_inc Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now