javascript 如果满足条件,如何“重新启动”if 语句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18643778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I "restart" if statement if a condition is met?
提问by Adrian Clark
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.
我对 javascript 很陌生。我想制作一个快速程序,使用我父亲教我的公式,生成一个人能够约会的最小年龄。在我的代码中,我有一个条件,如果我的 var (dateage) 不是数字,则要求用户输入一个数字。我希望程序然后重新询问变量赋值提示,直到给出一个数字。
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?
您可以看到,如果 dateage 不是数字,则会提醒用户。那时我希望再次出现询问用户年龄的提示。我怎样才能做到这一点?
回答by Paul S.
Put it in a functionso 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.
我使用 aconfirm
进行重新检查,因为这意味着您可以更轻松地摆脱无限循环的情况。如果你不想污染命名空间,你可以把它写成一个命名的IIFE,如果你不想把堆栈带过去,你可以通过一个setTimeout调用。
回答by Culyx
wrap the question and output in a while loop, breaking only when number has been entered
将问题和输出包装在一个 while 循环中,仅在输入数字时中断
回答by Praxis Ashelin
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();
回答by Niet the Dark Absol
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
.
在setTimeout
释放了当前调用堆栈,否则如果你设法无限循环,你可能会得到一个堆栈溢出错误restart
。
回答by wsc
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();