Generation

generate functionSat, 01 Apr 2023

Make a dice class - The class should have a throw method that simulates throwing a die. It returns the number thrown, but it also stores in the instance how many we have thrown, which can be retrieved by a getter function. We retrieve a value so that it is read-only. - Furthermore, this sows can be rendered anywhere in HTML. Which of course you can do by specifying renderTo in the constructor, and the cube will be created here. Translated with www.DeepL.com/Translator (free version)

function Dice() { this.value = 0; this.numberOfThrows = 0; this.renderTo = ""; } Dice.prototype.render = function () { document.querySelector(this.renderTo).innerHTML = this.value; }; Dice.prototype.throw = function () { this.numberOfThrows++; this.value = Math.ceil(Math.random() * 6); }; Dice.prototype.getNumberOfThrows = function () { return this.numberOfThrows; }; Dice.prototype.getValue = function () { return this.value; };

Javascript
Generate More

Questions about programming?Chat with your personal AI assistant