Meal Maker: Cannot read property 'push' of undefined


#1

I’m working on Meal Maker, and I have seen my problem posted many times here. After reviewing an example of working code from mft and comparing it side-by-side to mine, I think I have figured out what I did wrong.

I’m just looking for confirmation that I have correctly identified the culprit. My code follows:

const menu = {
  _courses: {
    _appetizers: [],
    _mains: [],
    _desserts: []
  },
  get courses() {
    return {
      appetizers: this._courses.appetizers,
      mains: this._courses.mains,
      desserts: this._courses.desserts
    };
  },
  get appetizers() {
    return this._appetizers;
  },
  get mains() {
    return this._mains;
  },
  get desserts() {
    return this._desserts;
  },
  set appetizers(appetizersIn) {
     this._appetizers.push(appetizersIn);
  },
  set mains(mainsIn) {
     this._mains.push(mainsIn);
  },
  set desserts(dessertsIn) {
     this._desserts.push(dessertsIn);
  },
  addDishToCourse (courseName, dishName, dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice
    };
    this._courses[courseName].push(dish);
  },
  getRandomDishFromCourse(courseName) {
    let dishes = menu._courses[courseName];
    let 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}, ${dessert.name}, and the total price is $${totalPrice}.`;
  }
};

menu.addDishToCourse('appetizers', 'Nachos', 6.95);
menu.addDishToCourse('appetizers', 'Chile Con Queso', 7.95);
menu.addDishToCourse('appetizers', 'Taquitos', 5.95);

menu.addDishToCourse('mains', 'Enchiladas', 9.95);
menu.addDishToCourse('mains', 'Fajitas for Two', 19.95);
menu.addDishToCourse('mains', 'Comida Deluxe', 13.95);

menu.addDishToCourse('desserts', 'Sopapillas', 4.95);
menu.addDishToCourse('desserts', 'Flan', 5.95);
menu.addDishToCourse('desserts', 'Tres Leches', 6.95);

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

If I am correct, my fault was in nesting my getters and setters outside the _courses property. Am I right? If not, what am I missing?


#2

This code I am posting will help you see any errors you may have in your code. My code works 100% so just compare it side by side to find out how to get it working.

const menu = {
_courses: {
appetizers: ,
mains: ,
desserts: ,
},
//Get
get appetizers(){
return this._appetizers;
},
get mains() {
return this._mains;
},
get desserts() {
return this._desserts;
},
get courses() {
return {
appetizers: this._courses.appetizers,
mains: this._courses.mains,
desserts: this._courses.desserts,
}
},
//Set
set appetizers(appetizer) {
this._appetizers.push(appetizer);
},
set mains(main) {
this._mains.push(main);
},
set desserts(dessert) {
this._desserts.push(dessert);
},
addDishToCourse(courseName, dishName, dishPrice) {
let dish = {
courseName: courseName,
dishName: dishName,
dishPrice: dishPrice,
};
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.dishPrice + main.dishPrice + dessert.dishPrice;
return Your meal is ${appetizer.dishName}, ${main.dishName}, ${dessert.dishName}. The price is ${totalPrice}.;
}
};
menu.addDishToCourse(‘appetizers’, ‘salad’, 8);
menu.addDishToCourse(‘appetizers’, ‘onion rings’, 11);
menu.addDishToCourse(‘appetizers’, ‘shrimp’, 13);
menu.addDishToCourse(‘mains’, ‘chicken’, 18);
menu.addDishToCourse(‘mains’, ‘beef’, 16);
menu.addDishToCourse(‘mains’, ‘chili’, 14);
menu.addDishToCourse(‘desserts’, ‘fudge brownie’, 7);
menu.addDishToCourse(‘desserts’, ‘dolce de leche’, 5);
menu.addDishToCourse(‘desserts’, ‘cake shake’, 4);

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


#3

Thanks for the helpful response!

I actually got a working form of the code and did the exact exercise you proposed. I wanted to confirm that I correctly identified what I did wrong.

Due to my own impatience and hubris, I charged ahead with the next project and applied what I thought I learned from my mistakes. Either I keep accidentally getting it right, or I correctly identified the problem.

Thanks again for replying!