Meal Maker - Bad Getters and maybe bad Push

I’m at a loss for what is going wrong with my code for the Meal Maker project. Originally I didn’t have anything in the getters and setters because it didn’t say to put anything there and had a problem with the push in the addDishToCourse method. So I watched the get help video and added in what it put for the getters and setters. Now I get an error message that it cannot read property appetizers in the get appetizers() method.

Any thoughts are appreciated.

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
    get appetizers() {return this._courses.appetizers},
    set appetizers(app) {this._courses.appetizers = app},
    get mains() {return this._courses.mains},
    set mains(ma) {this._courses.mains = ma},
    get desserts() {return this._courses.desserts},
    set desserts(des) {this._courses.desserts = des}
  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 index = Math.floor(Math.random() * dishes.length);
    return dishes[index];
  generateRandomMeal() {
    const appetizer = this.getRandomDishFromCourse("appetizers");
    const main = this.getRandomDishFromCourse("mains");
    const dessert = this.getRandomDishFromCourse("desserts");
    const cost = this._courses.appetizers[appetizer].price + menu._courses.mains[main].price + menu._courses.desserts[dessert].price;
    return `1st Course: ${}. 2nd Course: ${}. 3rd Course: ${}. Total Cost: ${cost}`;

menu.addDishToCourse("appetizers", "salad", 4.00);
menu.addDishToCourse("appetizers", "Mozzarella Stick", 4.50);
menu.addDishToCourse("appetizers", "Samplet PLatter", 7.00);

menu.addDishToCourse("mains", "steak", 14.00);
menu.addDishToCourse("mains", "chicken", 10.00);
menu.addDishToCourse("mains", "pasta", 9.00);

menu.addDishToCourse("desserts", "cake", 5.00);
menu.addDishToCourse("desserts", "pudding", 3.00);
menu.addDishToCourse("desserts", "pie", 4.00);

let meal = menu.generateRandomMeal();


It cannot read the getters and setters as you’ve included them in _courses. If you take these out, so you have

const menu = {
    _courses: {
      appetizers: [],
      mains: [],
      desserts: []},
    get appetizers() {return this.appetizers},
    set appetizers(app) {this.appetizers = app},
    get mains() {return this.mains},
    set mains(ma) {this.mains = ma},
    get desserts() {return this.desserts},
    set desserts(des) {this.desserts = des},
    get courses() {
      return {
        appetizers: this._courses.appetizers,
        mains: this._courses.mains,
        desserts: this._courses.desserts}

instead, then that should fix that error. This did end up giving me a different error, which I fixed by changing the cost in your generateRandomMeal() function to

const cost = appetizer.price + main.price + dessert.price;

With getRandomDishFromCourse() you have already specified the main path and so do not need the rest of the stuff before your appetizer.price. This makes the code work for me, hopefully that makes sense!

Had to make a few tiny tweaks after moving everything out of _courses, but works perfectly now. That was the main problem. I must have stared at this for a at least an hour and just didn’t see it.


1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.