[英]Don't know what's wrong with my IF statement (JAVASCRIPT)
I am trying to make a number guessing game where the user gets 3 chances to guess a number between 1 to 10 and after the third try if they don't manage to guess the number correctly, they'll lose and the number input becomes disabled .我正在尝试制作一个数字猜谜游戏,其中用户有 3 次机会猜测 1 到 10 之间的数字,并且在第三次尝试后,如果他们无法正确猜出数字,他们将输掉并且数字输入被禁用.
So my variable "tryNum" is meant to be the number of guesses the user gets which I set to be 3. And I then wrote my if statement whereby every time the user guesses the number incorrectly, the value of tryNum decreases by 1. And based on my code, by the time the user guesses 3 incorrect numbers, the input should become disabled and an error message should be displayed.所以我的变量“tryNum”是指用户猜测的次数,我设置为 3。然后我写了我的 if 语句,每次用户猜错数字时,tryNum 的值都会减少 1。并且根据我的代码,当用户猜出 3 个不正确的数字时,输入应该被禁用并且应该显示错误消息。 However, when I run my code, it takes 4 incorrect guesses instead of 3 for my code to execute.
但是,当我运行我的代码时,我的代码执行需要 4 次错误猜测而不是 3 次。
I am not sure why this is.我不确定这是为什么。 Any help would be greatly appreciated.
任何帮助将不胜感激。
Here is my code:这是我的代码:
let min = 1,
max = 10,
winningNum = 3;
tryNum = 3;
function btnClciked(){
guessedNum = parseInt(userInput.value);
if (tryNum > 0){
let status;
if (isNaN(guessedNum) || guessedNum < min || guessedNum > max ){
if(document.querySelector(".error-display")){
status = false;
} else {
status = true;
}
isNaN(guessedNum) ? createDivElement("You havn't entered any number!", status, "#eb992d","black", true) : createDivElement(`You must guess a number between ${min} and ${max}`, status, "#eb992d", "black", true);
}
else if (guessedNum === winningNum){
status = true;
userInput.disabled = true;
userInput.style.borderColor = "green";
submitBtn.value = "Play Again!";
submitBtn.className += "play-again";
createDivElement(`Congratulations! You guessed the winning number: ${winningNum} Correctly!`, status, "green", "white");
}
else {
status = true;
userInput.style.borderColor = "red";
userInput.value = "";
tryNum -= 1;
createDivElement(`${guessedNum} isn't the winning number! You have ${tryNum} more guesses left!`, status, "red", "white");
}
} else {
userInput.disabled = true;
status = true;
userInput.style.borderColor = "red";
submitBtn.value = "Play Again!";
submitBtn.className += "play-again";
createDivElement(`Game Over! The winning number was ${winningNum}.`, status, "red", "white");
}
}
When the user guesses it wrong, you should check if tryNum
is equal to 0
, in which case you should show that it is Game Over.当用户猜错时,你应该检查
tryNum
是否等于0
,在这种情况下你应该表明它是 Game Over。
function btnClciked(){
guessedNum = parseInt(userInput.value);
if (tryNum > 0){
let status;
if (isNaN(guessedNum) || guessedNum < min || guessedNum > max ){
if(document.querySelector(".error-display")){
status = false;
} else {
status = true;
}
isNaN(guessedNum) ? createDivElement("You havn't entered any number!", status, "#eb992d","black", true) : createDivElement(`You must guess a number between ${min} and ${max}`, status, "#eb992d", "black", true);
}
else if (guessedNum === winningNum){
status = true;
userInput.disabled = true;
userInput.style.borderColor = "green";
submitBtn.value = "Play Again!";
submitBtn.className += "play-again";
createDivElement(`Congratulations! You guessed the winning number: ${winningNum} Correctly!`, status, "green", "white");
}
else {
status = true;
userInput.style.borderColor = "red";
userInput.value = "";
tryNum -= 1;
if(tryNum != 0){
createDivElement(`${guessedNum} isn't the winning number! You have ${tryNum} more guesses left!`, status, "red", "white");
} else {
userInput.disabled = true;
status = true;
userInput.style.borderColor = "red";
submitBtn.value = "Play Again!";
submitBtn.className += "play-again";
createDivElement(`Game Over! The winning number was ${winningNum}.`, status, "red", "white");
}
}
}
}
Basically when you don't guess the number the third time the value of tryNum is 0 but you have to make another guess in order to trigger the first if statement and enter in the final else block.基本上,当您第三次没有猜到数字时,tryNum 的值是 0,但您必须再次猜测才能触发第一个 if 语句并进入最后一个 else 块。 The simplest solution is to put the final else block into an if statement below
tryNum-=1
to check if the variable is 0最简单的解决方法是将最后的else块放入
tryNum-=1
下面的if语句中,检查变量是否为0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.