0

I am designing a monopoly style digital game and want a dice rolling button so i tried this:

<button class="bttnrolldice" onclick="diceRoll()">Roll Dice</button><h1 id="rollresults"></h1> 
<script>
    function diceRoll() {
            var die1 =  math.ceil(math.random() * 7;
            var die2 =  math.ceil(math.random() * 7;
            document.getElementById("rollresults").innerHTML = (die1 + die2); }
</script>

Whenever I run the code, nothing shows up below the button. Can you give me a hand? (It might have something to do with the variables that equal js code, if so tell me how to fix this.)

19
  • I just want to Note that you are multiplying a random number between 0 and 1 by 7. Then rounding up. That will give you number 1 through 7 inclusively. You should multiply by 6 to get 1 through 6 (although its possible to still get 0 using random()) Commented Apr 7, 2017 at 21:27
  • wait could i do: document.getElementById("rollresults").innerHTML = ((die1 + 1) + (die2 + 1));? Commented Apr 7, 2017 at 21:32
  • You could use Math.ceil(6*(1 - Math.random())); which would include 1 through 6 but exclude 0 Commented Apr 7, 2017 at 21:34
  • That Makes Sense! Commented Apr 7, 2017 at 21:35
  • Math.random() returns a float between 0 (inclusive) and 1 (exclusive) which is why the subtraction fixes the problem Commented Apr 7, 2017 at 21:35

3 Answers 3

1

You are missing closing parenthesis on your Math.ceil functions

<button class="bttnrolldice" onclick="diceRoll()">Roll Dice</button><h1 
id="rollresults"></h1> 
<script>
    function diceRoll() {
       var die1 =  Math.ceil(Math.random() * 7);
       var die2 =  Math.ceil(Math.random() * 7);
       document.getElementById("rollresults").innerHTML = (die1 + die2); 
   }
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

I think what he wanted was Math.ceil(Math.random() *7 )
@MatthewCiaramitaro Thx did the switch of all Math now it should be correct
Yes omg i use w3 schools.com for reference and I completely missed that part. Thank You!
1

You are missing a right parentheses on line 4 and line 5.

When running your script in Google Chrome, press [F12] and the error will be described on the Console tab.

4 Comments

im on a laptop and the button for [F12] is the shortcut for airplane mode... btw im sort of new to js...
especially the math.something part of js
right click on Chrome in click inspect on the menu and it'll open the inspector. Extremely useful for JS debugging
i see now that you told me
0

Inspect element and check browser console for errors.

  1. It should be "Math" instead of "math".
  2. Closing parenthesis are missing in statements where die1 and die2 variables are declared.

1 Comment

Thank You! This Really Helped!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.