我试图设置一个“手指数”游戏,用户输入数字,只有5次尝试选择与随机数相同的数字。然后经过3次尝试,将提醒您还有2次尝试,然后1left,然后结束游戏,再试一次。请不要太猛烈地抨击我,但我的代码设置有误,我在某处缺少一个步骤,并且试图找出在哪里。
document.getElementById("check").onclick = function() {
//Get a random number between 1 and 5//
var randomNumber = Math.random();
randomNumber = randomNumber * 6;
randomNumber = Math.floor(randomNumber);
//Count the number of guesses the user has//
//User has five attempts to guess correct number//
var guess;
var guessCount = 0;
var numberOfGuesses = 5;
remainingAttempts = numberOfGuesses--;
//This is the value of what the user inputs. If the same as random number alert correct//
//If random number and user input doesnt match alert incorrect//
if (document.getElementById("guess").value == randomNumber) {
alert("Well done, got it!");
} else {
alert("That's not it! Try again");
}
remainingAttempts--;
//Show how many attempts left//
if (guessCount >= 3); {
console.log(remainingAttempts + " tries left");
}
//two guesses left//
console.log("Close but no dice." + remainingAttempts + "tries left")
//1 guess left//
console.log("Last try")
//Game over//
if (guessCount >= 5) {
alert("No more attempts left.Game Over");
return;
}
};
<title>How Many Fingers!</title>
<p> Guess how many fingers!</p>
<p><input type="text" id="guess"> <button id="check">Guess!</button></p>