Https://www.codecademy.com/courses/introduction-to-javascript/projects/meal-maker

Hi, I can keep getting a TypeError: Cannot read property ‘appetizers’ of undefined
whenever I try to run my code. I’ve searched the forum and have tried to use the methods provided that have helped other users but it hasn’t seemed to work for me. can you please help?

----- MY CODE ------

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: []
  },
  get appetizers(){
  return this._courses.appetizers;
  },
  get mains(){
  return this._courses.mains;
  },
  get desserts(){
  return this._courses.desserts;
  },
  set appetizers(appetizersIn){
  this._courses.appetizers = appetizersIn;
  },
  set mains(mainsIn){
  this._courses.mains = mainsIn;
  },
  set desserts(dessertsIn){
  this._courses.desserts = dessertsIn;
  },
  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._course[courseName];
    const randomIndex = Math.floor(Math.random() * dishes.length);
    return dishes[randomIndex]
  },
  generateRandomMeal() {
    const appetizers = this.getRandomDishFromCourse('appetizers');
    const mains = this.getRandomDishFromCourse('mains');
    const desserts = this.getRandomDishFromCourse('desserts');
    const totalPrice = appetizers.price + mains.price + desserts.price

    return `Your meal is ${appetizers.name}, ${mains.name}, ${desserts.name} The price is £${totalPrice}.`;
  }
};

menu.addDishToCourse('appetizers', 'Marak', 4.50)
menu.addDishToCourse('appetizers', 'Sambuus', 2)
menu.addDishToCourse('appetizers', 'Salaato', 3)
menu.addDishToCourse('mains', 'Federation', 13)
menu.addDishToCourse('mains', 'Bariis iyoo Haneed', 10)
menu.addDishToCourse('mains', 'Canjeero dilac bilaash', 9)
menu.addDishToCourse('desserts', 'Tiramisu', 5)
menu.addDishToCourse('desserts', 'Baklava', 3)
menu.addDishToCourse('desserts', 'Khat', 25)

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

console.log(meal.courses)

thanks in advance!


mod edited formatting

To the OP: This is a one time freebie. Format your code samples if you want to be taken seriously. Skipping over the new user reading is not advisable.

  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
    get courses(){
      return {
        appetizers: this.appetizers,
        mains: this.mains,
        desserts: this.desserts
      }
    }
  },

That’s the context the getter is written in. One might wish to consider that.

Hi @chefnoname! Per53us here,

Line 40 check it again. Make sure that it’s “this._courses”, instead of just “this._course” This will get rid of your error.

In the future, make sure to include your line where the error occurred with the error message for context. This will help people to assist you faster!

Also, you’re going to want to remove the last console.log() statement, as (as far as I can tell, meal._courses) will always return undefined.

1 Like