简体   繁体   English

为什么如果 function 不再运行带有数字的提示?

[英]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的自调用不可能被命中,因为ifelse 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.

相关问题 为什么我的函数提示(Javascript)不起作用? - Why does my function prompt(Javascript) doesn't work? 为什么 function 中的简单提示不起作用? - Why simple prompt in a function doesn't work? 为什么长号比短号运行此功能更快? - Why does this function run faster with long numbers than short numbers? 为什么当引用不存在时,firebase“on”“value”不会运行监听器功能,但“一次”“值”是什么? - Why does firebase “on” “value” not run the listener function when the reference doesn't exist, but “once” “value” does? CKEditor为什么不运行editorConfig函数 - Why doesn't CKEditor run editorConfig function 为什么此xhtml文件显示JS代码但不运行该功能 - Why does this xhtml file display JS code but doesn't run the function Yeoman递归提示,回调不运行 - Yeoman recursive prompt, callback doesn't run JavaScript - 函数内部的提示不会出现在屏幕上,但会出现在函数外部 - JavaScript - Prompt inside function doesn't appear on screen, but does outside the function 为什么 function 中的 function 不起作用? - Why does a function inside a function doesn't work? 为什么javascript函数在第一次运行时不更新DOM - Why doesn't javascript function update DOM on first run
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM