【问题标题】:JavaScript - Prompt while input is not a numberJavaScript - 输入不是数字时提示
【发布时间】:2016-08-01 20:29:16
【问题描述】:

我想向用户询问一个数字,而他的答案不是数字,问题将继续循环(作为警告框)。

我尝试使用 while 循环和 isNaN 来做到这一点,但我做错了。

这是我的 Js:

var correct = false;

do {
    var question = prompt("guess the number?");
    if (question !== isNaN) {
        document.write("your number is " + question);
        correct = true;
        break;
   }

} while(! correct);

【问题讨论】:

  • isNaN 是一个函数!
  • 哦,你是对的。我把它改成了 isNaN() 但还是不行。
  • 为什么我得到-1?有人可以解释一下吗?

标签: javascript loops


【解决方案1】:

几个错误

  • isNaN() 是一个函数:isNaN(NaN) 返回 true 而 isNaN(1) 或 isNaN("1") 返回 false
  • prompt() 总是返回一个字符串(从不NaN)

您可以使用unary + operator将prompt()的结果转换为数字,然后检查是否为NaN;

var question = prompt("guess the number?");
if (!isNaN(+question)) {
  ...

也就是说,如果您只是想查看字符串是否仅包含数字,isNaN() 不适合,因为空格("" 或 " " 或 "\n\t")将全部转换为 0并给你误报。

simple regex 会做正确的事;

var question = prompt("guess the number?");
var containsOnlyDigits = /^[0-9]+$/; // one or more of digits 0 to 9
if (containsOnlyDigits.test(question)) {
  ...

【讨论】:

    【解决方案2】:

    我将通过以下示例为您提供一些提示:

    <html>
     <head>
        <title></title>
        <script>
    
        var answer = "";
    
        do {
            answer = prompt("guess the number?");
        } while(!isNumber(answer));
    
        document.write("your number is " + answer);
    
        function isNumber(value) {
          var numberPattern = /^[0-9]+$/; // one or more of digits 0 to 9
          return numberPattern.test(value);
        }
    
        </script>
     </head>
     <body>
    
    </body>
    

    此处示例:https://plnkr.co/edit/ziysG36if9OTZahHfSbO

    • 没有必要创建一个名为正确的附加变量来检查条件是真还是假,因为您已经明确了条件 isNan(answer),因此 while 应该使用该条件“while (isNan(回答))”。

    • 当您编写代码时,您应该尽可能写得干净,如果您保存提示的结果,则更清楚地将变量命名为“answer”,因为您保存的是答案而不是问题,即方法调用。

    【讨论】:

    • 谢谢!您能解释一下为什么在循环外声明 var 答案并在内部再次调用它吗?而不是在循环内声明变量?谢谢
    • @xec 如果答案是一个数字,那么循环预计会运行一次,正如他在他的问题中所写的那样“虽然他的答案不是一个数字,但问题将继续循环(作为警报框)。 "。
    • @CodeChunk 已编辑,不需要在其作用域之外声明,其实就是while循环
    • 你能更详细地解释一下关于在循环外或循环内声明变量吗?
    • @CodeChunk 对于这个例子没有太大区别,在其他语言中你不能访问超出其严格范围的变量,这意味着你不应该能够访问“答案” while 如果它在内部被删除,但现在不适用。
    【解决方案3】:

    isNan 是一个函数 - 您需要通过在其后放置括号来调用它,然后将您的参数放在这些括号内。

    如果参数不是数字,isNaN 函数返回true,如果是则返回false。既然你想检查question 是否是一个数字,那么我们可以使用! 前缀反转布尔输出,如果question 是,这将导致if 语句的主体触发一个数字。

    var correct = false;
    
    do {
      var question = prompt("guess the number?");
      if (!isNaN(question)) {
        document.write("your number is " + question);
        correct = true;
        break;
      }
    } while (!correct);
    

    【讨论】:

      【解决方案4】:

      isNaN 是一个有一些特殊规则的函数。您应该首先尝试将输入解析为一个数字,然后在该结果上使用isNaN。 Number 如果从字符串到数字的转换失败,则返回 NaN。

      var input1 = "1234";
      var input2 = "abcd";
      
      alert(isNaN(Number(input1)));
      alert(isNaN(Number(input2)));

      【讨论】:

        【解决方案5】:

        如上所述 - isNan 是一个获取参数作为输入的函数。

        您不必在isNan 中的测试之前将提示转换为数字,正如您在文档中看到的那样,它也可以接受字符串。

        https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/isNaN#Examples

        该函数将在后台内部进行转换:

        你可以把 isNaN 想象成:

        isNaN = function(value) {
           Number.isNaN(Number(value));
        }
        

        这是一个适合你的代码 sn-p:

        var correct = false;
        
        do {
            var answer = prompt("guess the number?");
        
            // The isNaN function gets an number argument
            // You also need to check if there is value.
            // If the user click no ESC you will get null
            if (answer && !isNaN(answer)) {
        
                // You cant write to the document once its loaded.
                // it will clear any previous content in the page
                document.write("your number is " + answer);
                correct = true;
                break;
            }
        
        } while (!correct);
        

        【讨论】:

          【解决方案6】:

          这是一个类似的解决方案:

          for (let numTrue = false; !numTrue;) {
          	var question = prompt("Guess the Number!", "Number");
          	numTrue = !isNaN(question);
          }
          document.write("Your number is " + question);

          【讨论】:

            猜你喜欢
            • 2016-07-13
            • 1970-01-01
            • 2013-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-10-20
            • 2020-10-09
            • 1970-01-01
            相关资源
            最近更新 更多