javascript - Loop that prompts user until valid input -


question: how continuously prompt user when age invalid (below 0 above 200, or not-a-number)?

var userage = prompt("please enter age"); while (isnan(userage)){     alert("that's not number, please retry.");     var userage = prompt("please re-enter age."); }  if (userage == 0){     alert("so you're baby!"); } while( userage < 0  || userage >= 200){     alert("i think may lying age.");     var userage = prompt("please re-enter age, don't lie time.");     {continue} }  if (userage > 0 || userage < 200){     alert("that's age.");  }  </script> 

you can return while loop break statement. example:

var userage = prompt("please enter age"); while (true) {     if (isnan(userage)) {         alert("that's not number, please retry.");     } else if (userage < 0 || userage >= 200) {         alert("i think may lying age.");     } else {         break;     }     var userage = prompt("please re-enter age."); } if (userage == 0) {     alert("so you're baby!"); } else {     alert("that's age."); } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -