Created
February 17, 2016 22:52
-
-
Save patrickocoffeyo/8e7c289369f1ec09f147 to your computer and use it in GitHub Desktop.
Choose what to eat for lunch by rolling a jsdice.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file | |
* Contains dice with n sides. | |
*/ | |
'use strict'; | |
/** | |
* Defines Dice class. | |
* | |
* @class | |
* @classdesc Allows a dice to be created, and rolled. | |
*/ | |
class Dice { | |
/** | |
* Scaffolds class properties, sets up dice sides. | |
* @param {array} sides array of strings that represent dice sides. | |
* @returns {undefined} nothing. | |
*/ | |
constructor(sides) { | |
if (sides.constructor === Array && sides.length > 0) { | |
this.sides = sides; | |
} | |
else { | |
throw new Error('Sides must be an array with values, you dipshit'); | |
} | |
} | |
/** | |
* Rolls the dice! | |
* @returns {string} random side. | |
*/ | |
roll() { | |
return this.sides[Math.floor(Math.random() * this.sides.length)]; | |
} | |
} | |
// Create instance of dice object, pass it delicious ATX eats. | |
let game = new Dice([ | |
'Torchys', | |
'Bills', | |
'TacoDeli', | |
'KerbyLane', | |
'MadamMams', | |
'KoreaHouse', | |
'NeWorld' | |
]); | |
// Roll the dice and log the output. | |
console.log(game.roll()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment