-1

followed a tutorial just with different layout and names and still can't seem to find whats wrong?

<!DOCTYPE html>
<html>
  <head>
    <script>
      function myrandom() {
        var x = math.floor((math.random() * 10) + 1);
        document.getElementById("rand").innerHTML = x;
      }
    </script>
  </head>
  <body>
    <button onclick="myrandom()">Generate</button>
    <p id='rand'></p>
  </body>
</html>
J D
  • 73
  • 1
  • 7
  • Open your console. You will see errors that tell you what the problem is. (hint: many programming languages are case-sensitive) – CertainPerformance May 13 '18 at 03:39
  • @CertainPerformance I don't see your down vote on any other answers for the same reason. Why down vote only mine? Bully? – Hey24sheep May 13 '18 at 03:49

2 Answers2

3

Reason :

What you are doing wrong is that you are using math instead of Math. JavaScript is case sensitive and Math is defined while math is not.

Corrected :

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <button onclick="myrandom()">Generate</button>
    <p id='rand'></p>
    <script>
      var myrandom = () => {
        var x = ~~((Math.random() * 10) + 1);
        document.getElementById("rand").innerHTML = x;
      }
    </script>
  </body>
</html>

Also please note that it is better that you write the JS code at the end of the body tag and the script tag is the last one in the body tag.

Note :

Note that I edited the snippet provided by you.

The following edits were made

  • The script tag was moved at the end of body tag
  • I changed the function and used arrow syntax instead of normal declaration
  • Changed Math.floor into bitwise ~~

Resources :

Muhammad Salman
  • 403
  • 5
  • 17
-1

You have a typo, You need to capitalize the first letter of math -> Math.

<!DOCTYPE html>
<html>
  <head>
    <script>
      function myrandom() {
        var x = Math.floor((Math.random() * 10) + 1);
        document.getElementById("rand").innerHTML = x;
      }
    </script>
  </head>
  <body>
    <button onclick="myrandom()">Generate</button>
    <p id='rand'></p>
  </body>
</html>
Hey24sheep
  • 1,087
  • 6
  • 16