How to control the do / while infinite loop?
-
Dear all, it's my simple JS program. I can't figure out that why can't i stop the loop by using "guess==null". Please help!!
var target = prompt("Please enter a number small than 50:");
var guess = ""; var time = 0; /\* var guess2 = prompt("111"); var guess3 = window.prompt("222",guess3); document.write(typeof(guess)); document.write(typeof(guess2)); document.write(typeof(guess3)); \*/ do{ guess = prompt("Please Guess:"); if(guess == target || guess == null){ break; } if(guess < target){ alert("bigger!"); time++; continue; }else{ alert("smaller!"); time++; continue; } }while(true) if(guess==null){ document.write("the answer is:"+target); }else{ document.write("Good!共猜了:"+(time+1)); }
-
Dear all, it's my simple JS program. I can't figure out that why can't i stop the loop by using "guess==null". Please help!!
var target = prompt("Please enter a number small than 50:");
var guess = ""; var time = 0; /\* var guess2 = prompt("111"); var guess3 = window.prompt("222",guess3); document.write(typeof(guess)); document.write(typeof(guess2)); document.write(typeof(guess3)); \*/ do{ guess = prompt("Please Guess:"); if(guess == target || guess == null){ break; } if(guess < target){ alert("bigger!"); time++; continue; }else{ alert("smaller!"); time++; continue; } }while(true) if(guess==null){ document.write("the answer is:"+target); }else{ document.write("Good!共猜了:"+(time+1)); }
-
Dear all, it's my simple JS program. I can't figure out that why can't i stop the loop by using "guess==null". Please help!!
var target = prompt("Please enter a number small than 50:");
var guess = ""; var time = 0; /\* var guess2 = prompt("111"); var guess3 = window.prompt("222",guess3); document.write(typeof(guess)); document.write(typeof(guess2)); document.write(typeof(guess3)); \*/ do{ guess = prompt("Please Guess:"); if(guess == target || guess == null){ break; } if(guess < target){ alert("bigger!"); time++; continue; }else{ alert("smaller!"); time++; continue; } }while(true) if(guess==null){ document.write("the answer is:"+target); }else{ document.write("Good!共猜了:"+(time+1)); }
guess can return an empty string when you click on Ok without entering anything, guess will only be null if you click on cancel or if you close the prompt. Add guess == '' to your if statement or replace guess == null with isNaN(guess) that will check if the value is a legal number
-
Dear all, it's my simple JS program. I can't figure out that why can't i stop the loop by using "guess==null". Please help!!
var target = prompt("Please enter a number small than 50:");
var guess = ""; var time = 0; /\* var guess2 = prompt("111"); var guess3 = window.prompt("222",guess3); document.write(typeof(guess)); document.write(typeof(guess2)); document.write(typeof(guess3)); \*/ do{ guess = prompt("Please Guess:"); if(guess == target || guess == null){ break; } if(guess < target){ alert("bigger!"); time++; continue; }else{ alert("smaller!"); time++; continue; } }while(true) if(guess==null){ document.write("the answer is:"+target); }else{ document.write("Good!共猜了:"+(time+1)); }
A few things that might be helpful. You don't really need to test for the 'break' options (as your code appears) if you do something like this:
do {
if(something is true) {
do-this;
}
else
if(something else is true) {
do this;
}
. . . etc . . .
else // The only condition left
break;
// You don't need your "continue;" statements, which are implicit in your loop.} while(true)
This style will handle anything 'unexpected' - that is, anything you didn't intend
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein
"As far as we know, our computer has never had an undetected error." - Weisert
"If you are searching for perfection in others, then you seek disappointment. If you are seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010
-
A few things that might be helpful. You don't really need to test for the 'break' options (as your code appears) if you do something like this:
do {
if(something is true) {
do-this;
}
else
if(something else is true) {
do this;
}
. . . etc . . .
else // The only condition left
break;
// You don't need your "continue;" statements, which are implicit in your loop.} while(true)
This style will handle anything 'unexpected' - that is, anything you didn't intend
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein
"As far as we know, our computer has never had an undetected error." - Weisert
"If you are searching for perfection in others, then you seek disappointment. If you are seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010
-
-
guess can return an empty string when you click on Ok without entering anything, guess will only be null if you click on cancel or if you close the prompt. Add guess == '' to your if statement or replace guess == null with isNaN(guess) that will check if the value is a legal number