Meal maker project error

Hi,

Can someone please help with my code below, if I feel like its got loads of errors in it, but I’m currently getting the following error.

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:37
** this._courses[courseName].push(dish);**
** ^**

TypeError: Cannot read property ‘push’ of undefined
** at Object.addDishToCourse (/home/ccuser/workspace/learn**

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: []
  },
    get appetizers() {
      return this._courses.appetizers;
  },
    set appetizers(appetizerIn) {
      this._courses.appetizers = appetizerIn;
  },
    get mains() {
      return this._courses.mains;
  },
    set mains(mainsIn) {
      this._courses.mains = mainsIn;
  },
    get desserts() {
      return this._courses.desserts;
  },
    set desserts(dessertIn) {
      this._courses.appetizers = dessertIn;
  },
  get courses() {
    return {
     appetizers: this.appetizerIn,
     mains: this.mainsIn,
     desserts: this.dessertIn
		}
  },
  addDishToCourse: function (courseName, dishName, dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice
    }
		this._courses[courseName].push(dish);
  },
  getRandomDishFromCourse: function (courseName) {
    const dishes = this._courses[courseName];
    const randomIndex = Math.floor(Math.random * dishes.length);
    return dishes[randomIndex];
  },
  generateRandomMeal: function () {
    const appetizer = this.getRandomDishFromCourse(this._courses.appetizer);
    const main = this.getRandomDishFromCourse(this._courses.main);
    const dessert = this.getRandomDishFromCourse(this._courses.dessert);
    const totalPrice = courseName[0].price + courseName[1].price + courseName[2].price;
    
    return `Your meal is ${courseName[0].name}, ${courseName[1].name} and ${courseName[2].name} The price is $${totalPrice}.`
  }
}

menu.addDishToCourse('appetizer', 'soup', 7);
menu.addDishToCourse('appetizer', 'crab', 9);
menu.addDishToCourse('appetizer', 'bruscetta', 4);
menu.addDishToCourse('main', 'lamb shank', 12);
menu.addDishToCourse('main', 'steak and chips', 16);
menu.addDishToCourse('main', 'lamb curry', 13);
menu.addDishToCourse('dessert', 'ice cream', 5);
menu.addDishToCourse('dessert', 'fudge cake', 6);
menu.addDishToCourse('dessert', 'tiramasu', 4);

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

I know that this._courses[courseName].push(dish); is undefined and you cant push to an undefined value, but i have no idea how to solve it, as its the same as the code answer.

Thanks for your help,

No. Read the error carefully, it says that
this._courses[courseName]
is undefined

Is that the place where you expected something to be?
If so, what was responsible for putting it there?
Otherwise, where should it be looked for instead?

Note that you can print out various things to figure out what’s where.

Hi,

Thanks for the help, yeah i did understand, I should have been more clear in my thought process.

I’ve managed to solve it with some help. Console logging to debug in these data structures is a bit alien to me, made it more difficult to find the problem. Just need to get more confortable with them i guess.

Thanks for your help, solution below for anyone interested.

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

menu.addDishToCourse('appetizers', 'soup', 7);
menu.addDishToCourse('appetizers', 'crab', 9);
menu.addDishToCourse('appetizers', 'bruscetta', 4);
menu.addDishToCourse('mains', 'lamb shank', 12);
menu.addDishToCourse('mains', 'steak and chips', 16);
menu.addDishToCourse('mains', 'lamb curry', 13);
menu.addDishToCourse('desserts', 'ice cream', 5);
menu.addDishToCourse('desserts', 'fudge cake', 6);
menu.addDishToCourse('desserts', 'tiramasu', 4);

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