Why does the Math.random() keep updating onClick?

Samir

So upon learning a few JavaScript functions, I wanted to make a guessing game, this is my code: (EDITED)

<body>
  <script>
    var a;
    function rand(){
        var a = Math.floor((Math.random()*100)+1);
    }

    function random() {
      guess = document.getElementById('gess').value;
      if(guess < a) {
        document.getElementById('gus').innerHTML = 'To small!';

      }
      else if(guess > a) {
        document.getElementById('gus').innerHTML = 'To big!';
      }
      else if(guess == a) {
        document.getElementById('gus').innerHTML = 'Correct! The secret number is ' + a;

      } 

    }


  </script>
  <h1> Guess the secret number! </h1>
  <input type='text' id='gess'>
  <input type='button' onClick='random()' value='Guess!'>
  <input type='button' value='Reset Number' onClick='rand()'> 
  <br>
  <p id='gus'> </p>
</body>

What do I do, for the random number (a) to stop refreshing every time I click 'Guess', thank you in advance!

isaach1000

Do this:

var a = Math.floor((Math.random()*100)+1);
function random() {...}

That will store the random number before the random function is called. Otherwise, a will be reset every time.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why use Math.random(), and what it does

From Dev

Why does Math.floor(Math.random()) function always return "0"?

From Dev

Math.random/floor with a function and onclick?

From Dev

Why Math.random() type is number and Math.random is function?

From Dev

How does the distribution of Math.random()*50 + Math.random()*20 compare to Math.random()*70?

From Dev

Why does my HTML website search in VBA keep returning the same dataset rather than updating?

From Dev

Switch() does not work with Math.random() numbers

From Dev

Why does this not seem to be random?

From Dev

Why does this not seem to be random?

From Dev

In Java 8 why we cannot convert Math.random() to Math::random using method references

From Dev

Why does it keep returning "null"?

From Dev

Why does this function keep looping?

From Dev

Why does memtest keep freezing?

From Dev

Why does JavaScript have the Math object?

From Dev

Why JavaScript Math.random() returns same number multiple times

From Dev

What does Math.random() * i | 0 mean?

From Dev

What does Math.random() * i | 0 mean?

From Dev

Im making a program that makes random math, but it does not work

From Dev

How does Math.random() EXACTLY work in Java?

From Dev

Why does ld's KEEP() does not keep my symbols?

From Dev

Why isn't Math.floor((Math.random() * 999999) + 100000) reliable for producing 6 digit numbers?

From Dev

Concept of Math.floor(Math.random() * 5 + 1), what is the true range and why?

From Dev

Concept of Math.floor(Math.random() * 5 + 1), what is the true range and why?

From Dev

Why does the Ajax not updating the model correctly?

From Dev

Why can't I create a random array using _.map(new Array(n), Math.random)?

From Dev

How does Math.random() generate random numbers beyond it's "native" range?

From Dev

Is it possible for Math.random() === Math.random()

From Dev

Why does .lesshst keep showing up in my ~

From Dev

Why does JVM heap keep growing?

Related Related

  1. 1

    Why use Math.random(), and what it does

  2. 2

    Why does Math.floor(Math.random()) function always return "0"?

  3. 3

    Math.random/floor with a function and onclick?

  4. 4

    Why Math.random() type is number and Math.random is function?

  5. 5

    How does the distribution of Math.random()*50 + Math.random()*20 compare to Math.random()*70?

  6. 6

    Why does my HTML website search in VBA keep returning the same dataset rather than updating?

  7. 7

    Switch() does not work with Math.random() numbers

  8. 8

    Why does this not seem to be random?

  9. 9

    Why does this not seem to be random?

  10. 10

    In Java 8 why we cannot convert Math.random() to Math::random using method references

  11. 11

    Why does it keep returning "null"?

  12. 12

    Why does this function keep looping?

  13. 13

    Why does memtest keep freezing?

  14. 14

    Why does JavaScript have the Math object?

  15. 15

    Why JavaScript Math.random() returns same number multiple times

  16. 16

    What does Math.random() * i | 0 mean?

  17. 17

    What does Math.random() * i | 0 mean?

  18. 18

    Im making a program that makes random math, but it does not work

  19. 19

    How does Math.random() EXACTLY work in Java?

  20. 20

    Why does ld's KEEP() does not keep my symbols?

  21. 21

    Why isn't Math.floor((Math.random() * 999999) + 100000) reliable for producing 6 digit numbers?

  22. 22

    Concept of Math.floor(Math.random() * 5 + 1), what is the true range and why?

  23. 23

    Concept of Math.floor(Math.random() * 5 + 1), what is the true range and why?

  24. 24

    Why does the Ajax not updating the model correctly?

  25. 25

    Why can't I create a random array using _.map(new Array(n), Math.random)?

  26. 26

    How does Math.random() generate random numbers beyond it's "native" range?

  27. 27

    Is it possible for Math.random() === Math.random()

  28. 28

    Why does .lesshst keep showing up in my ~

  29. 29

    Why does JVM heap keep growing?

HotTag

Archive