Meal maker stuck eror

hi, I found my self stucked, and I don’t know where are the problems, I did double check but, still I can’t found my mistakes
here are my codes:

const menu = {
  _courses: {
    appertizers: [],
    mains: [],
    desserts: [],
  },
  get appetizers() {
    return this._course.appertizers;
  },
  get mains() {
    return this._course.mains;
  },
  get desserts() {
    return this._course.desserts;
  },
  set appetizers(appertizers) {
    this._course.appertizers = appertizers
  },
  set mains(mains) {
    this._course.mains = mains
  },
  set desserts(desserts) {
    this._course.desserts = desserts
  },
  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 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 order is ${appetizer.name}, ${main.name}, ${dessert.name}, and your total is ${totalPrice}.`
  }
};

menu.addDishToCourse('appetizers', 'apel', 6000);
menu.addDishToCourse('appetizers', 'jeruk', 5000);
menu.addDishToCourse('appetizers', 'semangka', 9000);

menu.addDishToCourse('mains', 'soto', 7000);
menu.addDishToCourse('mains', 'pecel', 5000);
menu.addDishToCourse('mains', 'lele', 8000);

menu.addDishToCourse('desserts', 'es krim', 2000);
menu.addDishToCourse('desserts', 'es teh', 3000);
menu.addDishToCourse('desserts', 'STMJ', 1500);

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

this is the error code prompt
/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:37
return this._courses[courseName].push(dish);
^

TypeError: Cannot read property ‘push’ of undefined
at Object.addDishToCourse (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:37:37)

thanks in advance :slightly_smiling_face:

JavaScript has this quirk where if you look up a property/key which does not exists, it gives undefined (Javascript should just give an error IMHO).

So what we should do is to add a .log() to inspect our code:

  addDishToCourse (courseName, dishName, dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice,
    };
    console.log(Object.keys(this._courses), courseName);
    return this._courses[courseName].push(dish);
  },

which allows us to see the available keys/properties:

["appertizers", "mains", "desserts"]

and the key you attempt to use:

"appetizers"

of the available keys, you have a spelling mistake/typo in appetizers. You wrote appertizers. This is why I generally recommend to have a spell-check on your code editor, unfortunately, that is not possible on codecademy.

thank you for your help!!! how can I did not see that :joy: :joy:

1 Like