function Question(category, value) {
// this = {};
this.category = category;
this.value = value;
// boolean (true = yes, false = no)
this.answer = confirm(value);
}
function Ingredients(category, values) {
this.category = category;
this.values = values;
}
function Pantry(ingredients) {
this.ingredients = ingredients;
}
Pantry.prototype.getIngredient = function(category) {
var ingredient;
this.ingredients.forEach(function(ingredients) {
if (ingredients.category === category) {
var random = Math.floor(Math.random() * ingredients.values.length);
ingredient = ingredients.values[random];
}
});
return ingredient;
}
function Preferences() {
this.values = [];
}
Preferences.prototype.add = function(preference) {
this.values.push(preference);
}
function Bartender() {
}
Bartender.prototype.createDrink = function(preferences) {
var output = 'Here\'s your drink: ';
preferences.values.forEach(function(pref) {
if (pref.answer === true) {
output += pantry.getIngredient(pref.category) + ', ';
}
});
alert(output);
}
var pantry = new Pantry([
new Ingredients('Strong', ['Glug of rum','slug of whisky','splash of gin']),
new Ingredients('Salty', ['Olive on a stick','salt-dusted rim','rasher of bacon']),
new Ingredients('Bitter', ['Shake of bitters','splash of tonic','twist of lemon peel']),
new Ingredients('Sweet', ['Sugar cube','spoonful of honey','splash of cola']),
new Ingredients('Fruity', ['Slice of orange','dash of cassis','cherry on top'])
]);
/*
{
ingredients: [
{ category: 'Strong', values: ['one', 'two', 'three'] },
{ category: 'Salty', values: ['one', 'two', 'three'] }
...
]
}
*/
// RUN!
var preferences = new Preferences();
// Question 1
var question = new Question('Strong', 'Do ye like yer drinks strong?');
preferences.add(question);
// Question 2
var question = new Question('Salty', 'Do ye like it with a salty...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.