This little program is supposed to have the browser guess at what the user inputs. It's "how many fingers". So i want it to force the user to use a number 5 or less.
If the input is larger than the range of options available to the program, the program will error out and break the browser. I've tried rearranging the nesting of the scripts in various ways to get a feel for how JS "thinks". Unfortunately, I still don't have a grasp of how Javascript is interpreting the flow of the code.
I use SQL at work and am familiar with the logic of PEMDAS in math. I think this is a similar situation where i'm missing something about the logical implications of the syntax. It seems to me like this is a very straightforward program.
I don't understand why the program doesn't execute the alert when my input meets the criteria for executing the IF clause. It works fine on the Else clause.
document.getElementById("guess").onclick = function() {
var a = document.getElementById("myNumber").value;
var b = 6;
var x = Math.random();
x = x * b;
x = Math.floor(x);
if (a > b) {
alert("you don't have that many fingers");
} else {
var gotit = false;
var guesses = 1;
while (gotit == false) {
if (a == x) {
gotit = true;
alert("Success! the answer is" + x + " or " + a + " it took me " + guesses + " guesses!");
} else {
guesses++;
var x = Math.random();
x = x * b;
x = Math.floor(x);
}
}
}
}
<p>How many fingers are you holding up?</p>
<input id="myNumber" />
<button id="guess">Guess!</button>
All help is greatly appreciated.
Aucun commentaire:
Enregistrer un commentaire