Meal maker, the final solution has unnecessary code

Hi all,

today I have started doing the ‘Meal Maker’ project and managed to finish it up and everything seems to work fine. See the full solution below:

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: []
  },
  get appetizers() {

  },
  get mains() {

  },
  get desserts() {

  },
  set appetizers(appetizerIn) {

  },
  set mains(mainIn) {

  }, 
  set desserts(dessertIn) {

  },
  get courses() {
    return {
      appetizers: this.appetizers,
      mains: this.mains,
      desserts: this.desserts
    }
  },
  addDishToCourse(courseName, dishName, dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice
    }

    this._courses[courseName].push(dish);
  },
  getRandomDishFromCourse(courseName) {
    let dishes = this._courses[courseName];
    let randomIndex = Math.floor(Math.random() * dishes.length);

    return dishes[randomIndex];
  },
  generateRandomMeal() {
    let appetizer = this.getRandomDishFromCourse('appetizers');
    let main = this.getRandomDishFromCourse('mains');
    let dessert = this.getRandomDishFromCourse('desserts');
    const totalPrice = appetizer.price + main.price + dessert.price;

    return `Your meal is ${appetizer.name}, ${main.name} and ${dessert.name}. The total price is $${totalPrice}.`;
  }
};

menu.addDishToCourse('appetizers', 'lettuce', 2.3);
menu.addDishToCourse('appetizers', 'Ceasar salad', 4.3);
menu.addDishToCourse('appetizers', 'wings', 4.5);

menu.addDishToCourse('mains', 'steak', 10.5);
menu.addDishToCourse('mains', 'pizza', 8.5);
menu.addDishToCourse('mains', 'lobster', 29.95);

menu.addDishToCourse('desserts', 'cake', 10);
menu.addDishToCourse('desserts', 'ice cream', 2.25);
menu.addDishToCourse('desserts', 'waffles', 5.5);

let meal = menu.generateRandomMeal();

console.log(meal);

However, I can’t understand why during this project I needed to create getter and setter methods for (appetizers, mains, desserts) if they are empty and don’t do anything. (Or at least there were no hints to insert anything in them).

Lastly, I tried commenting them out and the program still worked exactly the same. But if there are hints to create them that means that they definitely should be doing something I just can’t figure out what. Can someone please explain this to me?

Unfortunately, the non use of the getters and setters in this project have been the subject of many questions and complaints for a long while. You could, on your own, try to refactor the code to use the getters and setters. Since you have a courses getter, you could start by using it instead of accessing _courses directly in your code.

1 Like