最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I "restart" if statement if a condition is met? - Stack Overflow

programmeradmin2浏览0评论

I'm very new to javascript. I wanted to make a quick program that generates the youngest age possible a person is able to date given their age, using the formula my dad taught me. In my code I have a condition where if my var (dateage) isn't a number, the user is asked to please enter in a number. I want the program to then re-ask the variable assignment prompt until a number is given.

var dateage = prompt("How old are you?");

if(dateage >= 14){
    dateage = dateage/2 + 7;
    alert("The youngest you can date is " + dateage)
} else if(isNaN(dateage)){
   alert("Please enter in a number");
} else
   alert("You're too young to date.");

You can see that if dateage isn't a number, the user is alerted. At that point I want the prompt to appear again asking the user for their age. How can I do this?

I'm very new to javascript. I wanted to make a quick program that generates the youngest age possible a person is able to date given their age, using the formula my dad taught me. In my code I have a condition where if my var (dateage) isn't a number, the user is asked to please enter in a number. I want the program to then re-ask the variable assignment prompt until a number is given.

var dateage = prompt("How old are you?");

if(dateage >= 14){
    dateage = dateage/2 + 7;
    alert("The youngest you can date is " + dateage)
} else if(isNaN(dateage)){
   alert("Please enter in a number");
} else
   alert("You're too young to date.");

You can see that if dateage isn't a number, the user is alerted. At that point I want the prompt to appear again asking the user for their age. How can I do this?

Share Improve this question asked Sep 5, 2013 at 18:49 Adrian ClarkAdrian Clark 351 gold badge1 silver badge5 bronze badges 2
  • You'll need to use loops of one form or another. A simple do..while would be sufficient. – zzzzBov Commented Sep 5, 2013 at 19:07
  • jsfiddle/gCqqR – zzzzBov Commented Sep 5, 2013 at 19:13
Add a ment  | 

5 Answers 5

Reset to default 2

Put it in a function so you can re-invoke

function checkAge() {
    var dateage = prompt("How old are you?");

    if(dateage >= 14){
        dateage = dateage/2 + 7;
        alert("The youngest you can date is " + dateage)
    } else if(isNaN(dateage)){
       if (confirm("Please enter in a number")) checkAge();
    } else
       alert("You're too young to date.");
}
checkAge();

I used a confirm for the re-check because this means you can more easily escape from an infinite loop situation. If you don't want to pollute the namespace, you can write this as a named IIFE, and if you don't want to carry the stack over, you can invoke through a setTimeout.

You can put this in a function and simply keep recalling the function:

function askAge(){
   var dateage = prompt("How old are you?");

   if(dateage >= 14){
      dateage = dateage/2 + 7;
      alert("The youngest you can date is " + dateage)
   } else if(isNaN(dateage)){
      alert("Please enter in a number");
      askAge();
   } else
      alert("You're too young to date.");
}

askAge();

wrap the question and output in a while loop, breaking only when number has been entered

Personally, I like to have "restartable" functions look like:

(function() {
    var restart = arguments.callee; // "magic" property refers to current function
    if( somecondition) setTimeout(restart,1);
    else {
        // do actual stuff
    }
})();

The setTimeout released the current call stack, otherwise you could potentially get a stack overflow error if you manage to infinitely loop restart.

function checkAge() {
    var dateage = prompt("How old are you?");

    if(dateage >= 14){
        dateage = dateage/2 + 7;
        alert("The youngest you can date is " + dateage)
    } else if(isNaN(dateage)){
       if (confirm("Please enter in a number")) checkAge();
    } else
       alert("You're too young to date.");
}
checkAge();

发布评论

评论列表(0)

  1. 暂无评论