Meal Maker

Hello everyone. Tell me please why are the getter and setter methods on task 4 and 5 needed if with them or without them, the program works perfect? Thank You,

Summary
const menu ={
  _courses: {
    appetizers: [],
    mains: [] ,
    desserts: []
  } ,
  get appetizers() {
    return this._courses.appetizers;
  },
  set appetizers(appetizers) {
     return this._courses.appetizers = appetizers;
  },
  get mains() {
     return this._courses.mains;
  },
  set mains(mains) {
     return this._courses.mains = mains;
  },
  get desserts() {
     return this._courses.desserts
  },
  set desserts(dessets) {
     return this._courses.desserts = dessets;
  },
  
get courses() {
    return {
      appetizers: this.appetizers,
      mains: this.mains,
      desserts: this.desserts
    }
  },
  addDishToCourse (courseName, dishName, dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice,
    };
    return this._courses[courseName].push(dish);
  },
  getRandomDishFromCourse(courseName) {
   const dishes = this._courses[courseName];
    const randomIndex = Math.floor(Math.random() * dishes.length);
    return dishes[randomIndex]
  },
  generateRandomMeal() {
    const appetizer = this.getRandomDishFromCourse('appetizers');
    const main = this.getRandomDishFromCourse('mains');
    const dessert = this.getRandomDishFromCourse('desserts');
    const totalPrice = appetizer.price + main.price + dessert.price;
    return `Your meal is ${appetizer.name}, ${main.name} and ${dessert.name}, and the total price is ${totalPrice} USD`;
  }
}

menu.addDishToCourse('appetizers', 'a1', 34.00);
menu.addDishToCourse('appetizers', 'a2', 44.00);
menu.addDishToCourse('appetizers', 'a3', 64.00);

menu.addDishToCourse('mains', 'b1', 39.00);
menu.addDishToCourse('mains', 'b2', 86.00);
menu.addDishToCourse('mains', 'b3', 25.00);

menu.addDishToCourse('desserts', 'c1', 52.00);
menu.addDishToCourse('desserts', 'c2', 46.00);
menu.addDishToCourse('desserts', 'c3', 12.00);

const meal = menu.generateRandomMeal();
console.log(meal);

Hello,

This is a good observation, well done.

However the fact they are not used now, doesn’t mean they won’t be used in a different context. Say someone wants to know what meals exist on the menu, you would have to call the getters to find out.

It is common practice to implement getters and setters, and for a reason ;).

Happy coding!

Hello,

It is my first time posting in CC’s forum so apologies if this is not where I should post it (but please do give me some advice for future posts!)

My question is also about the meal maker project.

Why does appetizers, mains and desserts (in bold) have to be in strings when using the getRandomDishFromCourse method:

generateRandomMeal() {
const appetizer = this.getRandomDishFromCourse(‘appetizers’);
const mains = this.getRandomDishFromCourse(‘mains’);
const desserts = this.getRandomDishFromCourse(‘desserts’);
const totalPrice = appetizer.price + mains.price + desserts.price;
return Your meal consists of a ${appetizer.name} appetizer, a ${mains.name} main and a ${desserts.name} dessert. The price is £${totalPrice}.;
}
}

And are they referring to the appetizers/ mains/ desserts array inside the _courses object?

Thank you!

To answer the last question, first, yes. And since we know that all keys in JS are type ‘string’, we need to use a string to identify it with a variable…

const dishes = this._courses[courseName];

Note that we are only passing a key to the parameter. If it wasn’t a string it would raise an error.

1 Like