roll dice in javascript


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. var roll = {
  2. d4 : function() {
  3. return Math.floor(Math.random() * (4 - 1 + 1)) + 1;
  4. },
  5. d6 : function() {
  6. return Math.floor(Math.random() * (6 - 1 + 1)) + 1;
  7. },
  8. d8 : function() {
  9. return Math.floor(Math.random() * (8 - 1 + 1)) + 1;
  10. },
  11. d10 : function() {
  12. return Math.floor(Math.random() * (10 - 1 + 1)) + 1;
  13. },
  14. d12 : function() {
  15. return Math.floor(Math.random() * (12 - 1 + 1)) + 1;
  16. },
  17. d20 : function() {
  18. return Math.floor(Math.random() * (20 - 1 + 1)) + 1;
  19. }
  20. };
  21.  
  22. console.log("rolling D4 : " + roll.d4());
  23. console.log("rolling D20 : " + roll.d20());

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.