makispaiktis

Codecademy - 22nd Exercise (Meal Maker - Objects in JS)

Dec 23rd, 2019 (edited)
822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. 1.
  4. Start by creating an empty menu object.
  5. 2.
  6. Add a _courses property to your menu object and set its value to an empty object. This property will ultimately contain a mapping between each course and the dishes available to order in that course.
  7. 3.
  8. Create three properties inside the _courses object called appetizers, mains, and desserts. Each one of these should initialize to an empty array.
  9. 4.
  10. Create getter and setter methods for the appetizers, mains, and desserts properties.
  11. 5.
  12. Inside your menu object, create an empty getter method for the _courses property.
  13. 6.
  14. Inside the courses getter method, return an object that contains key/value pairs for appetizers, mains, and desserts.
  15. 7.
  16. Inside the menu object, we are going to create a method called .addDishToCourse() which will be used to add a new dish to the specified course on the menu.
  17. The method should take in three parameters: the courseName, the dishName , and the dishPrice.
  18. 8.
  19. The .addDishToCourse() method should create an object called dish which has a name and price which it gets from the parameters.
  20. The method should then push this dish object into the appropriate array in your menu‘s _courses object based on what courseName was passed in.
  21. 9.
  22. Now, we’re going to need another function which will allow us to get a random dish from a course on the menu, which will be necessary for generating a random meal.
  23. Create a method inside the menu object called .getRandomDishFromCourse(). It will take in one parameter which is the courseName.
  24. 10.
  25. There are a few steps in getting the .getRandomDishFromCourse() to work.
  26. Retrieve the array of the given course’s dishes from the menu‘s _courses object and store in a variable called dishes.
  27. Generate a random index by multiplying Math.random() by the length of the dishes array (This will guarantee that the random number will be between 0 and the length of the array)
  28. Round that generated number to a whole number by using Math.floor() on the result.
  29. Return the dish located at that index in dishes.
  30. 11.
  31. Now that we have a way to get a random dish from a course, we can create a .generateRandomMeal() function which will automatically generate a three-course meal for us. The function doesn’t need to take any parameters.
  32. The function should create an appetizer variable which should be the result of calling the .getRandomDishFromCourse() method when we pass in an appetizers string to it.
  33. Create a main variable and dessert variable the same way you created the appetizer variable, but make sure to pass in the right course type.
  34. Calculates the total price and returns a string that contains the name of each of the dishes and the total price of the meal, formatted as you like.
  35. 12.
  36. Now that we’ve finished our menu, object, let’s use it to create a menu by adding some appetizers, mains, and desserts with the .addDishToCourse() function. Add at least 3 dishes to each course (or more if you like!).
  37. 13.
  38. Once your menu has items inside it, generate a meal by using the .generateRandomMeal() function on your menu, and save it to a variable called meal. Lastly, print out your meal variable to see what meal was generated for you.
  39.  
  40. */
  41.  
  42.  
  43.  
  44. const menu = {
  45.  
  46.   _courses: {
  47.     appetizers: [],
  48.     mains: [],
  49.     desserts: [],
  50.     get getAppetizers(){
  51.       return this._appetizers;
  52.     },
  53.     get getMains(){
  54.       return this._mains;
  55.     },
  56.     get getDesserts(){
  57.       return this._desserts;
  58.     },
  59.     set setAppetizers(food){
  60.       this._appetizers = food;
  61.     },
  62.     set setMains(food){
  63.       this._mains = food;
  64.     },
  65.     set setDesserts(food){
  66.       this._desserts = food;
  67.     }
  68.   },
  69.  
  70.   get getCourses(){
  71.     return {
  72.       appetizers: this.getAppetizers,
  73.       mains: this.getMains,
  74.       desserts: this.getDesserts
  75.     };
  76.   },
  77.  
  78.   set setCourses(courses){
  79.     this._courses = courses;
  80.   },
  81.  
  82.   addDishToCourse(courseName, dishName, dishPrice)   {
  83.     let dish = {
  84.       name: dishName,
  85.       price: dishPrice
  86.     };
  87.     this._courses[courseName].push(dish);
  88.   },
  89.  
  90.   getRandomDishFromCourse(courseName){
  91.     let randomIndex = Math.floor(Math.random() * this._courses[courseName].length);
  92.     return this._courses[courseName][randomIndex];
  93.   },
  94.  
  95.   generateRandomMeal(){
  96.     const appetizer = this.getRandomDishFromCourse('appetizers');
  97.     const main = this.getRandomDishFromCourse('mains');
  98.     const dessert = this.getRandomDishFromCourse('desserts');
  99.     const price1 = appetizer.price;
  100.     const price2 = main.price;
  101.     const price3 = dessert.price;
  102.     const total = price1 + price2 + price3;
  103.     return (`You ordered: ${appetizer.name}: ${price1} euros.
  104. You ordered: ${main.name}: ${price2} euros.
  105. You ordered: ${dessert.name}: ${price3} euros.
  106. Total: ${total} euros.`);
  107.   }
  108.  
  109. };
  110.  
  111.  
  112. // Create meals/dishes with method 'addDishToCourse'
  113. // 1. Appetizers
  114. menu.addDishToCourse('appetizers', "Caesar's Salad", 4.5);
  115. menu.addDishToCourse('appetizers', "Tomato-onion Salad", 3.5);
  116. menu.addDishToCourse('appetizers', "Super Salad", 5.5);
  117. // 2. Mains
  118. menu.addDishToCourse('mains', "Chicken fillet", 8.5);
  119. menu.addDishToCourse('mains', "Souvlaki portion", 6.5);
  120. menu.addDishToCourse('mains', "Extreme Burger", 7.5);
  121. menu.addDishToCourse('mains', "Lamb Chops", 9.5);
  122. // 3. Desserts
  123. menu.addDishToCourse('desserts', "Apple pie", 3.5);
  124. menu.addDishToCourse('desserts', "Cheese-cake", 4.5);
  125. menu.addDishToCourse('desserts', "Strawberry cake", 3.5);
  126.  
  127. let meal = menu.generateRandomMeal();
  128. console.log(meal);
Add Comment
Please, Sign In to add comment