Meal Maker setter methods

For the meal maker project, why do we want setter methods for the appetizers, mains, and desserts. If we use them, and don’t enter an array of objects, but instead one object or something else, won’t it ruin the other functions on the menu object?

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
  get appetizers() {
    return this._courses.appetizers;
  set appetizers(appetizersIn) {
    this._courses.appetizers = appetizersIn;
  get mains() {
    return this._courses.mains;
  set mains(mainsIn) {
    this._courses.mains = mainsIn;
  get desserts() {
    return this._courses.desserts;
  set desserts(dessertsIn) {
    this._courses.desserts = dessertsIn;
  get courses() {
    return {
      appetizers: this.appetizers,
      mains: this.mains,
      desserts: this.desserts
  addDishToCourse(courseName, dishName, dishPrice) {
    let dish = {
      name: dishName,
      price: dishPrice
  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 ${}, ${}, and ${}. The price for your meal is $${totalPrice}.`;

menu.addDishToCourse('appetizers', 'Avocado Toast', 9.50);
menu.addDishToCourse('appetizers', 'Lump Crab Cake', 13.25);
menu.addDishToCourse('appetizers', 'Whole Roasted Cauliflower', 10.50);

menu.addDishToCourse('mains', 'Steak Salad', 19.50);
menu.addDishToCourse('mains', 'Hand-Rolled Lasagna', 19.50);
menu.addDishToCourse('mains', 'Roasted Vegetable Gnocchi', 16.50);

menu.addDishToCourse('desserts', 'Key Lime Pie', 3.50);
menu.addDishToCourse('desserts', 'Strawberry Cheesecake', 3.25);
menu.addDishToCourse('desserts', 'Pecan Pie', 3.50);

let meal = menu.generateRandomMeal();
menu.mains = {
  name: 'Filet Mignon',
  price: 33.75

So now menu.generateRandomMeal(); no longer works because the menu.mains is no longer an array.

A direct assignment to the backing variable such as this will overwrite the array that is meant to be the value. Use .push() instead so the array receives the new item.

1 Like

Thanks. I originally had that, but changed my code after watching the walk-through video. What you’re saying makes more sense to me .

1 Like

You will actually not use the setters and use addDishToCourse to add a meal to a course. I have left the setter as in the Walkthrough as a way to facilitate the scenario where one has to reset and bulk add meals.