JS中的数字猜谜游戏

时间:2015-11-01 19:07:44

标签: javascript html random

我想创建一个数字游戏,用户输入一个1-100的数字,然后脚本将尝试猜测用户输入的10个猜测。如果在10个猜测中它猜测它赢得的正确数字,否则用户获胜。到目前为止,我得到了正常的工作,除了我试图让它显示游戏结束时猜测的数量。因此,例如,如果进行10次猜测而不是它应该显示"猜测的数量:10"或猜测的数量:5" (如果猜到5)。



window.guessNum = function () {
    document.getElementById("result").innerHTML = "";
    var guesses = 10;
    var input = parseInt(document.getElementById("numberGuess").value);
    do {
        var randomNum = Math.floor(Math.random() * 100) + 1;
         
        document.getElementById("result").innerHTML += "I'll guess " + randomNum + " <br />";
        if (input == randomNum) {
        document.getElementById("result").innerHTML += "Our numbers match. I win!! :)<br /><br />";
        guesses = 0;
       }
       if (input < randomNum) {
       document.getElementById("result").innerHTML += "Your input is less than the number I'm thinking about. <br /><br />";
       } else if (input > randomNum) {
       document.getElementById("result").innerHTML += "Your input is greater than the number I'm thinking about.<br /><br />";
       }
       guesses = guesses - 1;
       }
       while (guesses > 0);
       if (input != randomNum) {
       document.getElementById("result").innerHTML += "I can't figure it out. I guess you win!<br /><br />";
       }
       document.getElementById("result").innerHTML += "Number of guesses: " +guesses+ " <br />";
    }
&#13;
<h1>Number Game</h1>
<div id="mainCont">
  <p>Number:
    <input type="text" id="numberGuess">
    <input type="button" onclick="guessNum();" value="Guess">
  </p>
  <div id="guess"></div>
  <div id="result"></div>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

你必须在每个循环中计算一个新的随机数(不仅仅是在开头)

答案 1 :(得分:0)

我注意到的第一个是下面的线已经不在循环中了

 var randomNum = Math.floor(Math.random() * 100) + 1;

如果数字相等则下面的行不存在,所以它会重复循环。

guesses = guesses - 1;

并且为了显示猜测及其结果,我将所有结果附加到单div

请参阅以下工作演示:

window.guessNum = function () {
    document.getElementById("result").innerHTML = "";
    var guesses = 10;
    var input = parseInt(document.getElementById("numberGuess").value);
    do {
        //guesses--;
          var randomNum = Math.floor(Math.random() * 100) + 1;
        document.getElementById("result").innerHTML += "<p>I 'll guess " + randomNum + " </p>";

        if (input < randomNum) {
            document.getElementById("result").innerHTML += "<p>Your input is less than the number I'm thinking about. </p>";
        } else if (input > randomNum) {
            document.getElementById("result").innerHTML += "<p>Your input is greater than the number I'm thinking about.</p> ";
        }
         guesses = guesses - 1;
    }
    while (guesses > 0);
    if (input != randomNum) {
        document.getElementById("result").innerHTML += "<p>I can 't figure it out. I guess you win!</p>";
    }
    if (input == randomNum) {
        document.getElementById("result").innerHTML += "<p>Our numbers match. I win!! :)</p>";
        guesses = 0;
    }
}
<h1>Number Game</h1>

<div id="mainCont">
    <p>Number:
        <input type="text" id="numberGuess">
        <input type="button" onclick="guessNum();" value="Guess">
    </p>
    <div id="guess"></div>
    <div id="result"></div>
</div>

演示:http://jsfiddle.net/kishoresahas/j0bbnhue/4/