随机数不在javascript范围内

时间:2017-03-10 14:31:00

标签: javascript html random range

我做了一个随机数猜猜游戏。用户必须设置可以生成随机数的最小和最大范围,并且它们获得的尝试次数取决于它们设置的范围的大小。因此,如果用户输入的最小值为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;
  }
}
&#13;
<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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:4)

value的{​​{1}} 总是一个字符串,当您使用其中一个操作数为字符串的input时,您会得到字符串连接,而不是补充。 (+会强制进行编号,但-不会。)所以说我们填写1和100.这样:

+

...获取randomNo = Math.floor(Math.random() * (maxRange - minRange)) + minRange; //random number generated 并得到99(到目前为止一直很好),将其乘以随机值得到(比方说)83,然后追加 maxRange - minRange得到"1"

您希望在将这些值转换为函数之前将它们转换为数字。有很多方法可以做到这一点(请参阅this answer for a rundown of them,但例如,一元"831"

+

现在该功能正在使用数字。

更新了代码段:

&#13;
&#13;
<button type="button" onclick="processingFunction(+document.getElementById('inputMinRange').value, +document.getElementById('inputMaxRange').value)">Set Range</button>
<!-- ---------------------------------------------^------------------------------------------------^                                -->
&#13;
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
console.log(minRange, maxRange, randomNo, attempts);
  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;
  }
}
&#13;
&#13;
&#13;

答案 1 :(得分:-1)

使用下面的代码转换你正在传递一个字符串,+将附加生成随机编号的minRange字符串。

randomNo = Math.floor(Math.random()*(maxRange - minRange))+ Math.round(minRange);

randomNo = Math.floor(Math.random()*(maxRange - minRange)) - ( - minRange);