英文:
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('startButton');
startButton.addEventListener('click', function() {
gameIsStarted = !gameIsStarted;
if (gameIsStarted) {
let n = 0;
while (n <= 0) {
n = Math.round(parseFloat(prompt('You have begun to play. What is the highest number you wish to be able to guess?')));
if (isNaN(n)) {
alert('That is not a number! Please enter a number.');
n = 0;
} else if (n < 1) {
alert('Please enter a number 1 and above.');
n = 0;
}
}
let secretNum = secretNumber(n);
guess(secretNum);
function secretNumber(n) {
return Math.floor(Math.random() * n) + 1;
}
function userGuess() {
let guessValue = parseInt(prompt('What do you think the number is?'));
guessValue = Math.round(guessValue);
return guessValue;
}
function guess(secretNum) {
let userGuessValue = userGuess();
while (userGuessValue !== secretNum) {
switch (true) {
case isNaN(userGuessValue):
alert('That is not a number!');
userGuessValue = userGuess();
break;
case userGuessValue <= 0:
alert('That number is too low. Pick a number that is at least 1.');
userGuessValue = userGuess();
break;
case userGuessValue > n:
alert('That number is not in range, try again.');
userGuessValue = userGuess();
break;
case userGuessValue > secretNum:
alert('No, try a lower number.');
userGuessValue = userGuess();
break;
case userGuessValue < secretNum:
alert('No, try a higher number.');
userGuessValue = userGuess();
break;
}
}
alert('You got it!');
}
}
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的情况。
罪魁祸首是parseInt
和parseFloat
。它们允许数字后面跟着其他字符。
要严格验证输入,请使用一元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('777$#@!*')); // 777
console.log(parseFloat('123.56abcd')); // 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(+'777$#@!*'); // NaN
console.log(+'777'); // 777
console.log(+'123.56abcd'); // NaN
console.log(+'123.56'); // 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('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');
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论