JavaScript: How to stop random number from regenerating? -
I'm a programming novice, so please bore me with me.
I am a child of a game to guess where a random number (between 1 and 50) is generated and you have to keep that number till you have guessed, unless you get that population. You get feedback about whether or not your estimation is too high or too low to better inform you of your estimation. The code I came down below, but the problem is, I can not point to random numbers. It seems that the number has been reproduced with every estimate. How can I make it so that only one number should be used for each estimate, until it is guessed correctly?
& lt ;! DOCTYPE html & gt; & Lt; Top & gt; & Lt; Meta http-equiv = "content-type" content = "text / html; charset = UTF-8" /> & Lt; Title & gt; Untitled document & lt; / Title & gt; & Lt; Link href = 'http: //fonts.googleapis.com/css? Family = source + sans + pro 'relay =' stylesheet 'type =' text / css' & gt; & Lt; Link href = "style.css" rel = "stylesheet" type = "text / css" /> & Lt; / Head & gt; & Lt; Body & gt; & Lt; Form id = "game" action = "javascript: void (checkGuess ())" & gt; & Lt; P class = "owlOne" & gt; I am thinking of a number between 1 and 50. & Lt; / P & gt; & Lt; Br class = "clear" /> & Lt; P class = "owlTwo" & gt; Input id = "estimate" type = "text" & gt; & Lt; Input id = "button" type = "submit" value = "estimate!" & Gt; & Lt; / P & gt; & Lt; Br class = "clear" /> & Lt; P class = "owlOne" & gt; & Lt; Span id = "response" & gt; Can you guess what it is? & Lt; / Span & gt; & Lt; / P & gt; & Lt; / Form & gt; & Lt; Script & gt; Function checkguo () {var estimate = number (document.getElementById ('estimation'). Value); // allocates the value to the estimated number of var number = math.flur (math.rendum) * 49 + 1); Generates a random number between 1 and 50 // // Estimated number is correct. If (estimate == number) {document.getElementById ("response"). InnerHTML = "Congratulations, you guessed correctly!"; } // The estimated number is within 5 above Else if (Estimate> number & amp; estimated & lt; = number + 5) {document.getElementById ("Response"). InnerHTML = "You are so close! Just a little less."; } // The approximate number is within 5 below. And if (guess lm; number-5 & amp; amp; estimate & gt; = 1) {document.getElementById ("feedback"). InnerHTML = "Too few! Think again!"; } // The approximate number is not greater than 50, but the above is less than 5. And if (Estimate, gt; 5 + & amp; amp; & amp; amp; amp; & amp; amp; amp; & amp; hypothesis & lt; = 50) {document.getElementById ("Feedback") innerHTML = "Too Much! Think Again"; } // approximate number is not within range 1 - 50 Else {document.getElementById ("response"). InnerHTML = "Remember, the number I'm thinking is between 1 and 50."; }} & Lt; / Script & gt; & Lt; / Body & gt; & Lt; / Html & gt;
Any suggestions?
move
var number = math.flur (mathematics. Random () * 49 + 1);
The Out function will again be generated on page load, the function is not called every time.
number
will become a global variable and will also be accessible within the function.
Comments
Post a Comment