【发布时间】:2021-04-14 02:39:01
【问题描述】:
它检查数字是否更大或更小,以及用户是否通过输入"q" 退出。但它不会捕获像"&, *, (, p, ]" 和"while(!guess)" 这样的无效输入。
let guess = window.prompt('guess the number!');
const rand = Math.floor(Math.random() * 10) + 1;
let tries = 0;
while (!guess) {
let guess = parseInt(prompt('enter a valid number'));
}
while (parseInt(guess) !== rand) {
tries += 1;
if (guess === 'q') {
console.log('you have exited');
break;
}
if (guess < rand) guess = prompt('enter higher!');
else guess > rand;
guess = prompt('enter lower!');
}
console.log(`correct number is : ${rand} and it took you ${tries} tries`);
【问题讨论】:
-
可能是因为您没有将初始猜测转换为整数。 "&" 的值是一个非空字符串,因此是真值,这意味着将跳过第一个 while 循环。
标签: javascript