gpt4 book ai didi

javascript - 随机数不在范围内javascript

转载 作者:行者123 更新时间:2023-11-30 11:40:55 24 4
gpt4 key购买 nike

<分区>

我做了一个随机数猜测游戏。用户必须设置可以生成随机数的最小和最大范围,并且他们获得的尝试次数会根据他们设置的范围大小而变化。因此,如果用户输入的最小值是 1,最大值是 100,则范围将除以 10 并四舍五入,让用户有 10 次尝试猜测数字。

我遇到的问题是生成的随机数总是超出设定范围,我不确定为什么。

我的 javascript 代码:

winner = "Well done, you guessed the number correctly";
loser = "Unfortunately you did not guess the number correctly. Game Over!";
higher = "Your guess was too low. Guess higher";
alreadyWon = "You have already guessed correctly. Press f5 to play again";
lower = "Your guess was too high. Guess lower";
gameWon = false;
counter = 0;

function processingFunction(minRange, maxRange) {
randomNo = Math.floor(Math.random() * (maxRange - minRange)) + minRange; //random number generated
attempts = Math.round((maxRange - minRange) / 10); //number of attempts generated
return (randomNo, attempts);
}

function showFunction(guess) {

if (gameWon == true) {
document.getElementById("output1").innerHTML = alreadyWon;
} else if (counter < attempts) {
if (guess == randomNo) {
document.getElementById("output1").innerHTML = winner;
gameWon = true;
} else if (guess > randomNo) {
document.getElementById("output1").innerHTML = lower;
} else {
document.getElementById("output1").innerHTML = higher;
}
counter++;
} else {
document.getElementById("output1").innerHTML = loser;
}
}
<center>
<h2>Random Number Guess</h2>
</center>
<h3>Enter in the minimum range and the maximum range. Accompanied by your first guess</h3>
Minimum Range:<input type="text" id="inputMinRange"></input> Maximum Range:<input type="text" id="inputMaxRange"></input>
<button type="button" onclick="processingFunction(document.getElementById('inputMinRange').value, document.getElementById('inputMaxRange').value)">Set Range</button>
<br><br>
<input type="text" id="guessInput"></input>
<button type="button" onclick="showFunction(document.getElementById('guessInput').value)">Guess</button>
<pre type="text" id="output1"></pre>

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com