[E] Roll a dice
This is a programming exercise where you can try to program a web page where you can roll a set of dices using JavaScript. There is a solution you can view, if you need to.
Roll dice
The example program "Roll dices in a webpage using JavaScript" shows you how you can roll a set of dices in the webpage, using JavaScript, events and manipulating the DOM.
Figure. This is how the example looks like when its done.
You should be ready to actual create the same code yourself, without the need of looking into the solution. Try and see how far you can go without looking at the solution.
Code with functions and modules
Once you have coded your own "dice rolling example" example, then proceed to modify the code and create a module to store the functions in.
Here is an example that shows how it can be done - Roll dice with functions and modules - but try on your own first.
Figure. This is how the example looks like when its done using modules.
Code with classes
A module can also contain a class as classical object oriented programming.
Can you rewrite your functions in the module to take the form as a class?
Try it on your own first, ther review a solution here - Roll dice with class.
This was fun, do more
If you are able to solve it all, then consider what you need to do to create a game of 21 using the dices instead of cards.
Each time you roll you get a new dice. Try to get as close to 21 as possible, but not higher.
Can you even play a game of 21 and meet the computer as the bank?
Try it out, it is a good coding exercise to see how far you can take it and a great exercise to play around with basic constructs with JavaScript in the browser.