Click here to Skip to main content
15,888,323 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This while loop loop loops forever on typing "yes" of "no". Please help!
HTML
<script type="text/javascript">

var y = prompt("Do you want to enter values, type 'Yes' or 'No'!");

while ((y.toUpperCase()!="YES") || (y.toUpperCase()!="NO"))
{
	if (y.toUpperCase()=="YES")
	{
 	alert(y);
	}
	else if (y.toUpperCase()=="NO")
	{
 	alert(y);
	}
	else
	{
	alert("You have type \"" +y+ "\" which is no option! Please type again");
        y = prompt("Do you want to enter values, type 'Yes' or 'No'!");
	}

}
</script>
Posted
v2
Comments
Sergey Alexandrovich Kryukov 15-Mar-14 21:48pm    
Use the debugger; and you will clearly see what's going on. But more importantly, just think. This is all you would need.
—SA

Try this
JavaScript
var y = prompt("Do you want to enter values, type 'Yes' or 'No'!");

while (y.toUpperCase()!="NO")
{
    if (y.toUpperCase()=="YES")
    {
    	alert(y);
        break;
    }
    else
    {
        alert("You have typed \"" +y+ "\" which is not an option! Please type again");
        y = prompt("Do you want to enter values, type 'Yes' or 'No'!");
    }
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Mar-14 12:10pm    
5ed, but frankly, this solution is not the best, but for the Q&A format...
Much better would be to repeat "think again" until OP get enlightened. This is one of the cases when nothing helps until the student does it all independently.
—SA
Richard MacCutchan 16-Mar-14 12:36pm    
I'm well aware of that, but it may help OP to think about why their code is stuck in a loop.
Sergey Alexandrovich Kryukov 16-Mar-14 12:59pm    
I was sure you were, this is just a general note for all readers.
Thank you.
—SA
That is because you have checked both YES and NO (using OR operator) in your while condition and allowed everything and again prompting.

You should probably change OR to AND and try.
 
Share this answer
 
v3
Comments
Maciej Los 15-Mar-14 16:48pm    
Sounds reasonable, but what you suggest to do?
BTW: +5!
Sergey Alexandrovich Kryukov 15-Mar-14 21:50pm    
Are you trying to hint about applying for McDonalds's job? :-)
—SA
Maciej Los 16-Mar-14 5:30am    
No, it was rhetorical question ;)
Thanks a lot Maciej. :)

Let's wait for OP's reply. :P
Maciej Los 16-Mar-14 5:31am    
Agree!
Thanks for the response . I fully understand your answers.Let's iterate:
I type "yes".
Then the while loop starts
it checks (y != "yes") result == false

then it checks (y !="no) result == true

Then it goes on iterating in the while loop.

Thanks guys!!
 
Share this answer
 
Comments
CHill60 16-Mar-14 13:35pm    
If you have something to add to your question use the Improve Question link - try to avoid posting your own comments as Solutions. If you do this before anyone else posts a solution, then your question will drop out of the list and fewer people will see it. Alternatively, use the Reply link next to a posted solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900