Skip to content

Instantly share code, notes, and snippets.

@patrickocoffeyo
Created February 17, 2016 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickocoffeyo/8e7c289369f1ec09f147 to your computer and use it in GitHub Desktop.
Save patrickocoffeyo/8e7c289369f1ec09f147 to your computer and use it in GitHub Desktop.
Choose what to eat for lunch by rolling a jsdice.
/**
* @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