我有一个while循环,用于检查数字是数字还是50或更低。目前它只运行循环,如果它是51或更多;不是如果是NaN!为了澄清,当midday
高于50或NaN时,它需要保持循环。
var midday = prompt("Enter midday temperature for day " + pn);
midday = parseFloat(midday);
while (Number.isNaN(midday) === false || midday > 50) {
//validate that the number is w/n an expected range.
//No minimum value as not expecting excessivly low input
alert("You entered a out out range or incorrect number")
var midday = prompt("Enter midday temperature for day " + pn);
midday = parseFloat(midday);
};
答案 0 :(得分:3)
我将暂时条件从Number.isNaN(midday) === false || midday > 50
更改为Number.isNaN(midday) || midday > 50
。
如果正午是一个数字,则测试Number.isNaN(midday) === false || midday > 50
将返回true。这意味着所有数字都将通过这种情况。
你想做的是检查中午是不是数字还是大于50
var midday = prompt("Enter midday temperature for day ");
midday = parseFloat(midday);
while (Number.isNaN(midday) || midday > 50) {
//validate that the number is w/n an expected range. No minimum value as not expecting excessivly low input
alert("You entered a out out range or incorrect number")
var midday = prompt("Enter midday temperature for day ");
midday = parseFloat(midday);
};
答案 1 :(得分:0)
isNaN()已经检查它是否不是数字 - >你希望它是真的。你只需要
while (Number.isNaN(midday) || midday > 50)
答案 2 :(得分:0)
尝试使用此代替Number.isNaN():
typeof(midday) === 'number'
isNaN()不检查var是否为数字,它检查var是否可以转换为不是NaN的数字,所以,如果你最终得到这样的场景:
isNaN('')
你会得到''不是NaN,破坏你的代码......
更多关于:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN