JavaScript-输入不是数字时提示
JavaScript - Prompt while input is not a number
我想向用户询问一个数字,而他的答案不是数字,这个问题将继续循环(作为一个警告框)。
我试着用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);
- JQM simpledialog:如何提示输入数字
- 显示数字键盘的Phonegap-js提示
- Javascript提示和警报输入一个数字,它将循环,您将输入数字以获得它的平均值
- JavaScript-输入不是数字时提示
- 不断提示用户一个数字,直到它落入某个范围
- Javascript:我可以根据提示输入在数组中找到数字的索引吗
- 工具提示中的 Google 日历图表数字格式
- 丰富的面孔 :如何在输入数字滑块工具提示的值旁边添加静态文本
- 如何使用 Google 脚本更改工具提示中数字的格式
- 设置高图表中工具提示数字的格式
- 更新 jQuery UI 工具提示上的数字
- 从提示和循环中获取数字
- 使用提示,选择数字的数目,将该数目添加到数组中,然后显示总数
- 尝试将文本字段中输入的两个数字相乘,并使用jquery提示答案
- 通过用户提示查找数字的阶乘
- 需要关于如何在JavaScript中考虑非常大的数字的提示/建议
- 在 JavaScript 中使用 for 循环编写代码,提示用户输入任意数量的数字,打印这些数字中最大和最小的数字
- 如何在提示框中只允许输入数字的javascript中进行验证
- 提示用户输入10个输入,将其放入数组中,找到平均值,并打印小于平均值的数字
- 如何编写javaScript函数,通过提示输入2个数字,并在警报中显示这些数字的总和