Meal maker

getting “TypeError: Cannot read property ‘push’ of undefined
at Object.addDishToCourse” error. Been troubleshooting for the past hour (scouring forums, googling, and watching the entire get help video and changing my code to match theirs exactly as far as I can tell) and can’t seem to figure out what the reason for the error is at this point.

Need to be consistent with spelling of your _courses object’s properties:

const menu = {
  _courses: {
    appetizers: [], //notice the s at the end of the property names
    mains: [],
    desserts: [],
  },

Look at the difference here:

menu.addDishToCourse('appetizer', 'wings', 5);
menu.addDishToCourse('appetizer', 'soup', 6);
menu.addDishToCourse('appetizer', 'salad', 2);

menu.addDishToCourse('main', 'sushi', 7);
menu.addDishToCourse('main', 'salmon', 10);
menu.addDishToCourse('main', 'burger', 9);

menu.addDishToCourse('dessert', 'cake', 8);
menu.addDishToCourse('dessert', 'ice cream', 5);
menu.addDishToCourse('dessert', 'cookies', 4);
  addDishToCourse(courseName, dishName, dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice
    };
    //Error message here
    return this._courses[courseName].push(dish); // You do not need a 'return' here since you aren't returning anything to the caller. You're just pushing the new dish to the array.
	},

Just need to add some s’s :wink:

sooo even when I take out the return statement so it’s just a push I still get the same error message. Originally my syntax was “this._courses[courseName].push(dish)”. I turned it into a return statement after watching and following along with the video walkthrough. And yes I have corrected my s’s :wink:

So, I found a few more errors in your code, but if you’re still getting the “TypeError: Cannot read property ‘push’ of undefined” then you must still not be using ‘appetizers’, ‘mains’, and ‘desserts’ in your calls to menu.addDishToCourse(). Normally I wouldn’t do this, but it’s late where I am, and I need to go to sleep, so I’m posting your code that I corrected, and it works below. Peak at it if you need to, but the errors are all just misspellings. Good luck!

P.S. I tried to place comments next to my corrections, but I may have missed one or two.

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
  },
  get appetizers() {
    return this._courses.appetizers;
  },
  set appetizers(appetizers){
        this._courses.appetizers = appetizers;
  },
  get mains() {
    return this._courses.mains;
  },
  set mains(mains) {
    this._courses.mains = mains;
  },
  get desserts() {
    return this._courses.desserts;
  },
  set desserts(desserts) {
    this._courses.desserts = desserts;
  },
  get courses() {
    return {
      appetizers: this.appetizers,
      mains: this.mains,
      desserts: this.desserts,
    }
  },
  addDishToCourse(courseName, dishName, dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice
    };
    //Error message here
    this._courses[courseName].push(dish);
    },
  getRandomDishFromCourse(courseName) {
    const dishes = this._courses[courseName];
    const randomDish = Math.floor(Math.random() * dishes.length);
    return dishes[randomDish];
  },
  generateRandomMeal() {
    const appetizer = this.getRandomDishFromCourse('appetizers'); //corrected
    const main = this.getRandomDishFromCourse('mains'); 
    const dessert = this.getRandomDishFromCourse('desserts'); //corrected
    
    const totalPrice = appetizer.price + main.price + dessert.price;
    
    return `Your meal is ${appetizer.name}, ${main.name}, ${dessert.name}. The price is ${totalPrice}.`
  }
  
};

menu.addDishToCourse('appetizers', 'wings', 5); //added s to each course
menu.addDishToCourse('appetizers', 'soup', 6);
menu.addDishToCourse('appetizers', 'salad', 2);

menu.addDishToCourse('mains', 'sushi', 7);
menu.addDishToCourse('mains', 'salmon', 10);
menu.addDishToCourse('mains', 'burger', 9);

menu.addDishToCourse('desserts', 'cake', 8);
menu.addDishToCourse('desserts', 'ice cream', 5);
menu.addDishToCourse('desserts', 'cookies', 4);

let meal = menu.generateRandomMeal(); //corrected

console.log(meal)

I see what I was doing wrong now, misunderstood what s’s were missing. I had been going on about 6 hours of coding without a break when I finally asked for help so I wasn’t totally functioning by any means! Sorry for the late reply but thank you for the help!

1 Like