Проблема в том, что операторы if и else if не работают. Независимо от того, какой входной сигнал, он отображает «Удачи в следующий раз». Если правильное число — 3 и я ввожу 3, все равно отображается «Удачи в следующий раз». Если это не число, оно также отображается следующим образом. Что здесь не так? Может ли кто-нибудь помочь?
Вот мой код:
Код: Выделить всё
// getting the value of a textbox
let guess = document.getElementById('text').value;
let GuessNumber = 3;
// creating a function to check whether the userinput and the variable is same on the click of a button.
document.getElementById("submit").onclick = () => {
// if the user input and the number is same just display gift
if (guess == GuessNumber) {
document.getElementById("P").innerHTML = "Gift";
}
// if the user input is not a number display 'it should be a number'
else if (isNaN(guess)) {
document.getElementById("P").innerHTML = "It should be a number";
}
// if the user input is a number greater than 10 or less than 1 display 'it is a number between 1 and 10.
else if (guess > 11 || guess < 0) {
document.getElementById("P").innerHTML = "It is a number between 1 and 10. Not beyond 10 or below"
}
// or if it is not same as the guess number display 'better luck next time'
else {
document.getElementById("P").innerHTML = "Better luck next time";
}
}Код: Выделить всё
Document
Number Guessing Game
Guess a number between 1 and 10
If your guess is correct we will give you a gift :)
submit
Подробнее здесь: https://stackoverflow.com/questions/719 ... ot-working
Мобильная версия