Running total of single die - Javascript -


i trying create application has die, singular dice, on screen. when click die, roll , display number. want keep running total of each roll.

so far have each time button clicked, image randomly changes. i'm confused how keep running total of rolls, however.

here code have far.

<html> <head> </head> <body>     <script>         function displaydie() {              var total = 0;             var num = ("src",(math.floor(math.random()*6)+1) + ".jpg")             document.getelementbyid("die").setattribute("src", (math.floor(math.random()*6)+1) + ".jpg")         }     </script>     <img id="die" alt="die"/>     <br/>     <input type="button" value="click me!" onclick="displaydie()"/>     <span id="total"/></span> </body> </html> 

use global variable store total value add value of current dice total. need store value of current dice in variable , use image , add.

var total = 0;  function displaydie() {     var num = (math.floor(math.random() * 6) + 1);     total += num;      document.getelementbyid("die").setattribute("src", num + ".jpg")     document.getelementbyid("total").innerhtml = total; } 

demo: fiddle


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -