JavaScript - 初学者问题

JavaScript - begginner questions

我在 JavaScript 中的第一个猜数“游戏”遇到了一些困难。有人可以看看并指导我做错了什么吗?不久前开始使用该语言..

功能分配给一个按钮

<input id="box;" class="btn" ; type="button" value="Guess" onClick="check()">Click to start !

用户(玩家)必须按下按钮,在 3 次尝试中猜出数字 (1-10),然后再玩一次或不玩。我每次尝试都说“数字更高”,但最终结果是随机的,即使你选择了 10。

var hiddenNum;
var attemps;

hiddenNum = Math.floor(Math.random() * 10);
hiddenNum = hiddenNum + 1;
attemps = 0;


    function check(guess) {
        window.prompt("Please enter the number between 1 and 10", "10");

        if (hiddenNum == guess) {
            window.alert("Congratulations! You guessed correctly !");

            again = window.prompt("Would you like to try again? Enter Y or N.", "Y");

            if (again == "N" || again == "n") {
                window.alert("Thanks for trying. Goodbye.");
                window.close();
            } else {
                window.alert("The number has been randomized.");
                window.location.reload();
            }

        } else {
            attemps = attemps + 1;

            if (hiddenNum < guess) {
                result = "lower";
            } else {
                result = "higher";
            }

            window.alert("Guess number " + attemps + " is incorrect. The number is " + result + ".");

        }

        if (attemps >= 3) {
            window.alert("Sorry, you have run out of guesses! The number was " + hiddenNum);

            again = window.prompt("Would you like to try again? Enter Y or N.", "Y");

            if (again == "N" || again == "n") {
                window.alert("Thanks for trying. Goodbye.");
                window.close();
            } else {
                window.alert("The number has been randomized.");
                window.location.reload();
            }

        }

    }

您将 check 函数设计为将猜测传递给它,但您只是调用它而没有这样做。所以你所有的比较都是针对 undefined.

您还使用 window.prompt 来进行实际猜测。您需要做的是将提示返回的值保存到一个变量中,然后进行比较。检查代码段。

var hiddenNum;
var attemps;

hiddenNum = Math.floor(Math.random() * 10);

hiddenNum = hiddenNum + 1;
attemps = 0;

function check() {
  let guess = window.prompt("Please enter the number between 1 and 10", "10");

  if (hiddenNum == guess) {
    window.alert("Congratulations! You guessed correctly !");

    again = window.prompt("Would you like to try again? Enter Y or N.", "Y");

    if (again == "N" || again == "n") {
      window.alert("Thanks for trying. Goodbye.");
      window.close();
    } else {
      window.alert("The number has been randomized.");
      window.location.reload();
    }
  } else {
    attemps = attemps + 1;

    if (hiddenNum < guess) {
      result = "lower";
    } else {
      result = "higher";
    }

    window.alert(
      "Guess number " + attemps + " is incorrect. The number is " + result + "."
    );
  }

  if (attemps >= 3) {
    window.alert(
      "Sorry, you have run out of guesses! The number was " + hiddenNum
    );

    again = window.prompt("Would you like to try again? Enter Y or N.", "Y");

    if (again == "N" || again == "n") {
      window.alert("Thanks for trying. Goodbye.");
      window.close();
    } else {
      window.alert("The number has been randomized.");
      window.location.reload();
    }
  }
}
<input id="box;" class="btn" ; type="button" value="Guess" onClick="check()">Click to start 

您需要分配提示

var hiddenNum;
var attemps;

hiddenNum = Math.floor(Math.random() * 10);
hiddenNum = hiddenNum + 1;
attemps = 0;



    function check() {
    
        let guess = window.prompt("Please enter the number between 1 and 10", "10");
      
        if (hiddenNum == guess) {
            window.alert("Congratulations! You guessed correctly !");

            again = window.prompt("Would you like to try again? Enter Y or N.", "Y");

            if (again == "N" || again == "n") {
                window.alert("Thanks for trying. Goodbye.");
                window.close();
            } else {
                window.alert("The number has been randomized.");
                window.location.reload();
            }

        } else {
            attemps = attemps + 1;

            if (hiddenNum < guess) {
                result = "lower";
            } else {
                result = "higher";
            }

            window.alert("Guess number " + attemps + " is incorrect. The number is " + result + ".");

        }

        if (attemps >= 3) {
            window.alert("Sorry, you have run out of guesses! The number was " + hiddenNum);

            again = window.prompt("Would you like to try again? Enter Y or N.", "Y");

            if (again == "N" || again == "n") {
                window.alert("Thanks for trying. Goodbye.");
                window.close();
            } else {
                window.alert("The number has been randomized.");
                window.location.reload();
            }

        }

    }
<input id="box;" class="btn" ; type="button"  value='guess'onClick="check()">Click to start!
User (player) have to press the button, guess the number (1-10) within 3 attemps and then play again or not. Every attemp I do it says "number is higher" but at the end result is random, even if you chose 10.

您应该存储 window.prompt 中的值。在您的代码中,您只是在函数的参数中使用 guess 作为 window.prompt 的值,这是不正确的。

Syntax 对于 window.promp:

result = window.prompt(message, default);

此外,我已经缩短了您的代码并将重复的项目存储在一个函数中,以使其不那么混乱。

function smallcheck() {
  let again = window.prompt("Would you like to try again? Enter Y or N.", "Y");
  if (again.toUpperCase() == "N") {
    window.alert("Thanks for trying. Goodbye.");
    window.close();
  } else {
    window.alert("The number has been randomized.");
    window.location.reload();
  }
}

function check() {
  let guess = window.prompt("Please enter the number between 1 and 10", "10");
  console.log(guess)
  if (hiddenNum == guess) {
    window.alert("Congratulations! You guessed correctly !");

    smallcheck()

  } else {
    attempts++;

    if (hiddenNum < guess) {
      result = "lower";
    } else {
      result = "higher";
    }
    window.alert("Guess number " + attempts + " is incorrect. The number is " + result + ".");

  }
  if (attempts >= 3) {
    window.alert("Sorry, you have run out of guesses! The number was " + hiddenNum);

    smallcheck()

  }

}

注意到您从未声明过 'result'。所以我把那个变量变成了一个三元运算符来进一步缩短你的代码。另外,我添加了模板文字 :)

let hiddenNum = Math.floor(Math.random() * 10);
//console.log(hiddenNum);
hiddenNum = hiddenNum;
let attempts = 0;

const nextRound = function () {
  let again = window.prompt('Would you like to try again? Enter Y or N.');
  if (again.toUpperCase() === 'N') {
    window.alert("Thank's for trying. Later nerd");
    window.close();
  } else {
    window.alert('Try to guess the new number, nerd.');
    window.location.reload();
  }
};

function check() {
  let guess = prompt('Please enter the number between 1 and 10');
  console.log(guess);

  if (hiddenNum === guess) {
    alert('Congratulations! You guessed correctly !');

    nextRound();
  } else {
    attempts++;

    let result = hiddenNum < guess ? 'lower' : 'higher';

    alert(`Guess number is ${attempts} is incorrect. The number is ${result}.`);
  }
  if (attempts >= 3) {
    alert(`Sorry, you have run out of guesses! The number was ${hiddenNum}`);

    nextRound();
  }
}
<div>
      <input
        id="box"
        class="btn"
        style="
          display: inline-block;
          border-radius: 2em;
          box-sizing: border-box;
          color: white;
          background-color: blueviolet;
          cursor: pointer;
        "
        type="button"
        value="Guess"
        onClick="check()"
      />Click to start !
    </div>