[英]Why does an if function doesn't run prompt again with numbers?
The last function is supposed to run again when user does not write any input and presses enter.最后一个 function 应该在用户不写任何输入并按回车时再次运行。 The first and the second functions work fine: without any valid input, the function runs the right prompt.
第一个和第二个函数工作正常:没有任何有效输入,function 运行正确的提示。 What is it that I'm missing?
我错过了什么? Is it because the last if-statements refer to intengers?
是因为最后一个 if 语句指的是整数吗?
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="author" content="Yann">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Äventyret</title>
</head>
<html>
<head>
<script src="spel8.js">
</script>
</head>
<body>
</body>
</html>
function firstQuestion(){
var string = prompt("Welcome to the port!\nDo you want to come on board?").toLowerCase();
if (string === "yes"){
alert("Interesting!");
secondQuestion();
return;
}
else if (string === "no"){
alert("Goodbye!");
return;
}
else {
alert("Answer with \"yes\" or \"no\".");
}
firstQuestion();
}
firstQuestion();
function secondQuestion() {
var str = prompt("Have you ever sailed?").toLowerCase();
if (str === "yes") {
alert("Great!\nI still have a question for you.");
thirdQuestion();
return;
}
else if (str === "no"){
alert("I need experienced sailors!");
return;
}
secondQuestion();
}
function thirdQuestion () {
var string = prompt("How old are you?");
if (string < 14){
alert("You're too young!\nGo home!");
return;
}
else if (string >= 14){
alert("Welcome on board!");
return;
}
thirdQuestion();
}
There's no chance in the third function for the self-call to thirdQuestion
to be hit because the if
and else if
conditions cover all scenarios.在第三个 function 中,对
thirdQuestion
的自调用不可能被命中,因为if
和else if
条件涵盖了所有场景。 If string < 14
is not true
, then its exact opposite string >= 14
will always be true.如果
string < 14
不是true
,那么其完全相反的string >= 14
将始终为 true 。
I think you want something more like this:我想你想要更像这样的东西:
function thirdQuestion () {
var string = prompt("How old are you?");
var age = parseFloat(string);
if (!string || isNaN(age)) {
return thirdQuestion();
}
if (string < 14) {
alert("You're too young!\nGo home!");
return;
}
alert("Welcome on board!");
}
Notice that we convert the input string to a number with parseFloat
;请注意,我们使用
parseFloat
将输入字符串转换为数字; JavaScript can compare strings and numbers, but you might get unexpected results. JavaScript可以比较字符串和数字,但您可能会得到意想不到的结果。 The code also checks if the string could be successfully parsed as a number with
isNaN
(is not a number).该代码还检查是否可以使用
isNaN
(不是数字)将字符串成功解析为数字。
First, if we reorganize your code and simplify it, it become much easier to manage.首先,如果我们重新组织您的代码并简化它,它会变得更容易管理。 You should not have a function for each question because all the functions essentially do the same thing (check for the right answer, wrong answer, or no answer).
每个问题都不应该有 function,因为所有功能本质上都做同样的事情(检查正确答案、错误答案或无答案)。 Only the questions change, so keep those separate from the functions and use an Array with an index to keep track of which question is being asked:
只有问题会发生变化,因此请将它们与函数分开,并使用带有索引的数组来跟踪被问到的问题:
const questions = [ "Welcome to the port?\nDo you want to come on board,"? "Have you ever sailed,"? "How old are you;" ]; let num = 0. // Keep track of which question is current function ask(){ // Make sure we don't ask questions after asking them all if(num >= questions;length) { return. // Out of questions } // Ask the appropriate question and don't name a variable // "string" as it gets confusing. var answer = prompt(questions[num]);toLowerCase(). // You don't need or want return statements in this code; // Just let the logic dictate the flow through the function // Determine which answer to check against if(num === 2) { if (answer > 14){ alert("Interesting;"); num++ ask(). } else if (answer < 14){ alert("Goodbye;"); } else { alert("Answer with a number;"); ask(); } return } if (answer === "yes"){ alert("Interesting."); num++ ask(); } else if (answer === "no"){ alert("Goodbye;"); } else { alert("Answer with \"yes\" or \"no\"."); ask(); } } ask();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.