JavaScript-输入不是数字时提示

JavaScript - Prompt while input is not a number

本文关键字:提示 数字 输入 JavaScript-      更新时间:2024-04-20

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

我试着用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(NaN)返回true,而isNaN(1)isNaN("1")返回false
  • prompt()始终返回字符串(从不返回NaN

您可以使用一元+运算符将prompt()的结果转换为数字,然后检查它是否为NaN

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

也就是说,如果你只是想看看字符串是否只包含数字,那么isNaN()就不太合适了,因为空白(""" ""'n't")都会转换为0,并给你带来误报。

一个简单的正则表达式将做正确的事情;

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

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

<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

  • 没有必要创建一个名为correct的附加变量来检查条件是真是假,因为您已经清除了条件isNan(answer),所以while应该使用该条件"while(isNan))"。

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

isNan是一个函数-您需要调用,方法是在其后面放括号,然后将参数放在括号内。

如果参数不是数字,则isNaN函数返回true;如果参数是数字,则返回false。由于您想检查question是否为数字,因此我们可以使用!前缀反转布尔输出,这将导致if语句的主体在question为数字时触发。

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

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

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

如上所述,isNan是一个将参数作为输入的函数。

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

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

该功能将在幕后进行内部转换:

你可以把isNaN想象成:

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

这里有一个代码片段,它将为您工作:

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);

这里有一个类似的解决方案:

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