I'm new at coding, just started learning javascript and I wanted to try and write a simple game to test out my skills. I apparently have very few because the code for a basic game doesn't work.
In this game, the user is prompted to generate a random number by clicking a button, once they click the button, they can enter their guess and submit. Once they submit their guess, they can click the last button to see if their guess was correct.
Unfortunately, when I try to run the code, it skips over the if else statement, no matter what I plug in and I can't figure out why. I would much appreciate the help. Please don't just give me a better code, I know there are a ton of easier ways I could have written it. But being able to spot and sort out bugs on a simple program like this would probably help me with spotting bugs in my own code in the future.
Thanks!!!
This is the code....don't make fun of me, I'm new to this.
<!DOCTYPE html>
<html>
<body>
<button onclick="genNum()"> Random Number </button>
<p id="demo"></p>
<form id="form1">
Guess: <input name="userGuess" type="number" size="20">
</form>
<button onclick="outputnum()"> Submit </button>
<button onclick="guessNum()"> Run Guess Num Function </button>
</body>
var ranNum;
function genNum() {
var ranNum = Math.floor(Math.random() * 101);
document.getElementById("demo").innerHTML =
ranNum;
}
function outputnum() {
x = document.getElementById("form1");
userguess = x.elements["userGuess"].value;
}
function guessNum() {
if (ranNum == userguess) {
alert("You are 70");
} else if (ranNum < userguess) {
alert("Less than 70");
} else {
alert("More than 70");
}
}
</script>
</html>
Aucun commentaire:
Enregistrer un commentaire