NaN在这段代码中允许数字和字母的组合。

huangapple go评论63阅读模式
英文:

NaN is allowing a combination of numbers and letters in this code

问题

为什么NaN允许我的代码中组合数字和字母?

let gameIsStarted = false;
let guesses = [];

const startButton = document.getElementById('startButton');

startButton.addEventListener('click', function() {
  gameIsStarted = !gameIsStarted;

  if (gameIsStarted) {
    let n = 0;
    while (n <= 0) {
      n = Math.round(parseFloat(prompt('您已开始游戏。您希望猜测的最大数字是多少?')));
      if (isNaN(n)) {
        alert('那不是一个数字!请输入一个数字。');
        n = 0;
      } else if (n < 1) {
        alert('请输入1或更高的数字。');
        n = 0;
      }
    }

    let secretNum = secretNumber(n);
    guess(secretNum);

    function secretNumber(n) {
      return Math.floor(Math.random() * n) + 1;
    }

    function userGuess() {
      let guessValue = parseInt(prompt('您认为这个数字是多少?'));
      guessValue = Math.round(guessValue);
      return guessValue;
    }

    function guess(secretNum) {
      let userGuessValue = userGuess();
      while (userGuessValue !== secretNum) {
        switch (true) {
          case isNaN(userGuessValue):
            alert('那不是一个数字!');
            userGuessValue = userGuess();
            break;
          case userGuessValue <= 0:
            alert('那个数字太低了。请选择至少为1的数字。');
            userGuessValue = userGuess();
            break;
          case userGuessValue > n:
            alert('那个数字不在范围内,请重试。');
            userGuessValue = userGuess();
            break;
          case userGuessValue > secretNum:
            alert('不,尝试一个较低的数字。');
            userGuessValue = userGuess();
            break;
          case userGuessValue < secretNum:
            alert('不,尝试一个更高的数字。');
            userGuessValue = userGuess();
            break;
        }
      }
      alert('你猜对了!');
    }
  }
  gameIsStarted = false;
});

我已经尝试使用元字符,问了我认识的每个人,但它仍然允许像13a或17w这样的输入。当我查看NaN的文档时,它在那里工作,所以为什么在我的代码中没有实现?

英文:

Why is NaN allowing a combination of numbers and letters in my code?

let gameIsStarted = false;
let guesses = [];
const startButton = document.getElementById(&#39;startButton&#39;);
startButton.addEventListener(&#39;click&#39;, function() {
gameIsStarted = !gameIsStarted;
if (gameIsStarted) {
let n = 0;
while (n &lt;= 0) {
n = Math.round(parseFloat(prompt(&#39;You have begun to play. What is the highest number you wish to be able to guess?&#39;)));
if (isNaN(n)) {
alert(&#39;That is not a number! Please enter a number.&#39;);
n = 0;
} else if (n &lt; 1) {
alert(&#39;Please enter a number 1 and above.&#39;);
n = 0;
}
}
let secretNum = secretNumber(n);
guess(secretNum);
function secretNumber(n) {
return Math.floor(Math.random() * n) + 1;
}
function userGuess() {
let guessValue = parseInt(prompt(&#39;What do you think the number is?&#39;));
guessValue = Math.round(guessValue);
return guessValue;
}
function guess(secretNum) {
let userGuessValue = userGuess();
while (userGuessValue !== secretNum) {
switch (true) {
case isNaN(userGuessValue):
alert(&#39;That is not a number!&#39;);
userGuessValue = userGuess();
break;
case userGuessValue &lt;= 0:
alert(&#39;That number is too low. Pick a number that is at least 1.&#39;);
userGuessValue = userGuess();
break;
case userGuessValue &gt; n:
alert(&#39;That number is not in range, try again.&#39;);
userGuessValue = userGuess();
break;
case userGuessValue &gt; secretNum:
alert(&#39;No, try a lower number.&#39;);
userGuessValue = userGuess();
break;
case userGuessValue &lt; secretNum:
alert(&#39;No, try a higher number.&#39;);
userGuessValue = userGuess();
break;
}
}
alert(&#39;You got it!&#39;);
}
}
gameIsStarted = false;
});

I have tried to use Metacharacters, I have asked everyone I know, but this will allow things like 13a or 17w. When I looked up the documentation on NaN, it worked there, so why is it not implementing in my code?

答案1

得分: 0

这将允许类似于13a或17w的情况。

罪魁祸首是parseIntparseFloat。它们允许数字后面跟着其他字符。

要严格验证输入,请使用一元number coercion

然后,根据您需要的数字类型执行其他检查。

函数 testNumber(值){
    console.log('Value:',value);
    console.log('> integer OR fractional:',Number.isFinite(value));
    console.log('> integer:',Number.isInteger(value));
    console.log('> fractional:',Number.isFinite(value)&& value!= Math.floor(value));
}

testNumber(+'777$#@!*');
testNumber(+'777.888');
testNumber(+'1234');

英文:

> this will allow things like 13a or 17w

The culprits are parseInt and parseFloat. They allow digits to be followed by other characters.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

console.log(parseInt(&#39;777$#@!*&#39;)); // 777
console.log(parseFloat(&#39;123.56abcd&#39;)); // 123.56

<!-- end snippet -->

To validate the input strictly, use unary number coersion.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

console.log(+&#39;777$#@!*&#39;); // NaN
console.log(+&#39;777&#39;); // 777
console.log(+&#39;123.56abcd&#39;); // NaN
console.log(+&#39;123.56&#39;); // 123.56

<!-- end snippet -->

Then you can perform other checks depending on what sort of number you need.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function testNumber (value) {
console.log(&#39;Value:&#39;, value);
console.log(&#39;&gt; integer OR fractional:&#39;, Number.isFinite(value));
console.log(&#39;&gt; integer:&#39;, Number.isInteger(value));
console.log(&#39;&gt; fractional:&#39;, Number.isFinite(value) &amp;&amp; value != Math.floor(value));
}
testNumber(+&#39;777$#@!*&#39;);
testNumber(+&#39;777.888&#39;);
testNumber(+&#39;1234&#39;);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月2日 05:21:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定