[英]How can I call a function within another function and make it run an if statement with a variable?
How can I make it so that when I type in the prompt, the value is stored as a number and refers to the second function?我怎样才能使它在我输入提示时,将值存储为一个数字并引用第二个 function?
function driversAge()
{
validation();
var age = prompt("What is your age?");
}
function validation()
{
if(Number(age) === 18){
alert("You are 18");
}
else if(Number(age) < 18){
alert("you are below 18");
}
else if(Number(age) > 18){
alert("You are over 18");
}
}
driversAge();
You could have your driversAge
function return the value, then pass it to your validation function.您可以让您的
driversAge
function 返回该值,然后将其传递给您的验证 function。
function driversAge () { return prompt('What is your age?') } function validation(age) { if (age > 18) { alert("Over 18") } else { alert("Not over 18"); } } const age = driversAge(); validation(age);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.