I made a random number guessing game in which you can determine the maximum value and the minimum value. I checked my entire code twice but can't seem to find a solution to why it won't work. I made this script linked to a HTML file that explains the document.getElementById("randNum").innerHTML
part. I also want to say I am very new to coding so I might've overlooked something simple. Anyways, here's my code.
我做了一个随机数猜测游戏,你可以在其中确定最大值和最小值。我检查了整个代码两次,但似乎无法找到解决方案,为什么它不起作用。我将此脚本链接到HTML文件,该文件解释了document.getElementById(“randNum”)。innerHTML部分。我还想说我对编码很新,所以我可能会忽略一些简单的事情。无论如何,这是我的代码。
var max = prompt("Max guessing number?");
if (max <= 2) {
document.getElementById("randNum").innerHTML = "Invalid entry";
} else {
var min = prompt("What's the smallest number you can guess?");
if (min >= (max - 1) || min <= 0) {
document.getElementById("randNum").innerHTML = "Invalid entry";
} else {
number = Math.floor(Math.random() * (max - min + 1) + min);
guess = prompt("What's your guess?");
if (guess > max || guess < min) {
document.getElementById("randNum").innerHTML = "Invalid entry";
} else if (guess == number) {
document.getElementById("randNum").innerHTML = "Good job! You guessed the number!";
} else {
document.getElementById("randNum").innerHTML = "Wrong! The answer was " + number + "!";
}
}
}
EDIT: I changed how the random number generator works but I'm still getting an error. And to add more detail, the page says Invalid entry
编辑:我改变了随机数发生器的工作方式,但我仍然收到错误。并添加更多详细信息,页面显示无效条目
2 个解决方案
#1
0
Two issues:
The data type returned by prompt
is string, so calculations might go wrong if you don't explicitly convert it to number. You can do this by applying the unitary +
operator, like +prompt('....');
prompt返回的数据类型是字符串,因此如果您没有将其显式转换为数字,则计算可能会出错。您可以通过应用单元+运算符来完成此操作,如+ prompt('....');
Secondly, the way to get random numbers between min and max should be different. Change this:
其次,在min和max之间获取随机数的方法应该是不同的。改变这个:
number = Math.floor((Math.random() * max) + min);
to:
number = Math.floor(Math.random() * (max - min + 1) + min);
The corrected code:
更正后的代码:
var max = +prompt("Max guessing number?");
if (max <= 2) {
document.getElementById("randNum").innerHTML = "Invalid entry";
} else {
var min = +prompt("Wat is het kleinste nummer dat je kan raden?");
if (min >= (max - 1) || min <= 0) {
document.getElementById("randNum").innerHTML = "Invalid entry";
} else {
number = Math.floor(Math.random() * (max - min + 1)) + min;
console.log(min, max, number, (max - min + 1));
guess = +prompt("What's your guess?");
if (guess > max || guess < min) {
document.getElementById("randNum").innerHTML = "Invalid entry";
} else if (guess == number) {
document.getElementById("randNum").innerHTML = "Good job! You guessed the number!";
} else {
document.getElementById("randNum").innerHTML = "Wrong! The answer was " + number + "!";
}
}
}
<div id="randNum" ></div>
#2
0
This seems to be really wrong for me:
这对我来说似乎是错的:
number = Math.floor((Math.random() * max) + min);
So, you are telling the random number generator to generate a number that is a maximum of max
and add with it the min
value. Say, if I give two numbers:
因此,您告诉随机数生成器生成一个最大值为max的数字,并添加最小值。说,如果我给两个数字:
15, 18
And if the Math.random() * max
is equal to 17
, then it becomes:
如果Math.random()* max等于17,那么它变为:
17 + 15 = 32
You might need to do this way:
您可能需要这样做:
number = Math.floor(Math.random() * (max - min + 1) + min);
The above code ensures that, the random number is not more than the difference between the max
and min
, and when we add the min
with the value, we have the random number generated between the range.
上面的代码确保随机数不超过max和min之间的差值,当我们将min添加到值时,我们在范围之间生成随机数。
This will make sure that the number is between max
and min
.
这将确保数字介于最大值和最小值之间。
#1
0
Two issues:
The data type returned by prompt
is string, so calculations might go wrong if you don't explicitly convert it to number. You can do this by applying the unitary +
operator, like +prompt('....');
prompt返回的数据类型是字符串,因此如果您没有将其显式转换为数字,则计算可能会出错。您可以通过应用单元+运算符来完成此操作,如+ prompt('....');
Secondly, the way to get random numbers between min and max should be different. Change this:
其次,在min和max之间获取随机数的方法应该是不同的。改变这个:
number = Math.floor((Math.random() * max) + min);
to:
number = Math.floor(Math.random() * (max - min + 1) + min);
The corrected code:
更正后的代码:
var max = +prompt("Max guessing number?");
if (max <= 2) {
document.getElementById("randNum").innerHTML = "Invalid entry";
} else {
var min = +prompt("Wat is het kleinste nummer dat je kan raden?");
if (min >= (max - 1) || min <= 0) {
document.getElementById("randNum").innerHTML = "Invalid entry";
} else {
number = Math.floor(Math.random() * (max - min + 1)) + min;
console.log(min, max, number, (max - min + 1));
guess = +prompt("What's your guess?");
if (guess > max || guess < min) {
document.getElementById("randNum").innerHTML = "Invalid entry";
} else if (guess == number) {
document.getElementById("randNum").innerHTML = "Good job! You guessed the number!";
} else {
document.getElementById("randNum").innerHTML = "Wrong! The answer was " + number + "!";
}
}
}
<div id="randNum" ></div>
#2
0
This seems to be really wrong for me:
这对我来说似乎是错的:
number = Math.floor((Math.random() * max) + min);
So, you are telling the random number generator to generate a number that is a maximum of max
and add with it the min
value. Say, if I give two numbers:
因此,您告诉随机数生成器生成一个最大值为max的数字,并添加最小值。说,如果我给两个数字:
15, 18
And if the Math.random() * max
is equal to 17
, then it becomes:
如果Math.random()* max等于17,那么它变为:
17 + 15 = 32
You might need to do this way:
您可能需要这样做:
number = Math.floor(Math.random() * (max - min + 1) + min);
The above code ensures that, the random number is not more than the difference between the max
and min
, and when we add the min
with the value, we have the random number generated between the range.
上面的代码确保随机数不超过max和min之间的差值,当我们将min添加到值时,我们在范围之间生成随机数。
This will make sure that the number is between max
and min
.
这将确保数字介于最大值和最小值之间。