JSFiddle - React, Tailwind, and code Playground

by Chad Drummond

JavaScript

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...