SHARE
TWEET

Untitled

a guest Oct 19th, 2017 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const menu = {
  2.   _courses: {
  3.     _appetizers:[],
  4.     _mains: [],
  5.     _desserts: [],
  6.     get appetizers() {
  7.       return this._appetizers;
  8.     },
  9.     set appetizers(appetizerIn) {
  10.        this._appetizers.push(appetizerIn);
  11.     },
  12.     get mains() {
  13.        return this._mains;
  14.     },
  15.     set mains(mainIn) {
  16.        this._mains.push(mainIn);
  17.     },
  18.     get desserts() {
  19.        return this._desserts;
  20.     },
  21.     set desserts(dessertIn) {
  22.        this._desserts.push(dessertIn);
  23.     }
  24.   },
  25.   get courses() {
  26.     return {
  27.       appetizers: this._courses.appetizers,
  28.       mains: this._courses.mains,
  29.       desserts: this._courses.desserts
  30.     };
  31.   },
  32.   addDishToCourse(courseName, dishName, dishPrice) {
  33.     const dish = {
  34.       name: dishName,
  35.       price: dishPrice
  36.     };
  37.     this._courses[courseName].push(dish);
  38.   },
  39.   getRandomDishFromCourse(courseName) {
  40.     const dishes = this._courses[courseName];
  41.     const randomIndex = Math.floor(Math.random() * dishes.length);
  42.     return dishes[randomIndex];
  43.   },
  44.   generateRandomMeal: function() {
  45.     const appetizer = this.getRandomDishFromCourse('appetizers');
  46.     const main = this.getRandomDishFromCourse('mains');
  47.     const dessert = this.getRandomDishFromCourse('desserts');
  48.     const totalPrice = appetizer.price + main.price + dessert.price;
  49.     return `Your meal is ${appetizer.name}, ${main.name}, ${dessert.name}. The price is $${totalPrice}.`;
  50.   }
  51. };
  52.  
  53. menu.addDishToCourse('appetizers', 'Caesar Salad', 8.50);
  54. menu.addDishToCourse('mains', 'Scallopina al Funghi', 24.50);
  55. menu.addDishToCourse('desserts', 'tiramisu', 6.50);
  56.  
  57. let meal = menu.generateRandomMeal();
  58. console.log(meal);
RAW Paste Data
Top