Click to See Complete Forum and Search --> : how can I make this so when someone enters valid number or cancels it stops looping.
2 peachy
05-19-2003, 10:37 PM
this code indefinately promps user to enter a value between 0 and 9.
how do I make it so the user is no longer prompted if a valid number is enter or the user clicks on cancel ?
var k = 0
while(true)
{
k = prompt(“enter a value between 0 and 9”,k);
}
AdamBrill
05-19-2003, 10:47 PM
Try this:var k = 0;
while(true){
k = prompt("enter a value between 0 and 9",k);
if(k==null){
//user pressed cancel
break;
}
if(k>=0 && k<10){
//k is between 0 and 9
break;
}
}
2 peachy
05-19-2003, 10:56 PM
Thankyou so much for you help... now I guess I will try to get my cookie thingy to work....
jeffmott
05-19-2003, 11:18 PM
code:--------------------------------------------------------------------------------var k = 0;
while(true){
k = prompt("enter a value between 0 and 9",k);
if(k==null){
//user pressed cancel
break;
}
if(k>=0 && k<10){
//k is between 0 and 9
break;
}
}
--------------------------------------------------------------------------------A little better (follows good programming practices)...
var k;
do {
k = prompt("Enter a value between 0 and 9", k);
} while (isNaN(k) || k < 0 || k > 9);
AdamBrill
05-20-2003, 07:07 AM
Yes, I know... I was just trying to write it so that it would be easy to understand... ;)