Learn Javascript Project Meal Maker

Welcome to the Get Help category!

This is where you can ask questions about your code. Some important things to remember when posting in this category :slight_smile:

  • Learn how to ask a good question and get a good answer!
  • Remember to include a link to the exercise you need help with!
  • If someone answers your question, please mark their response as a solution :white_check_mark:
  • Once you understand a new concept, come back and try to help someone else!

Hi, my name is Joe I am working on the Javascript beginners course and coming towards the end of the objects module. I am doing the meal maker project and I can’t find where I am going wrong. Any help would be greatly appreciated.

Thank you.

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: []
  get appetizers() {
    return this._courses.appetizers;
  set appetizers(appitizerIn) {
    this._courses.appetizers = appetizersIn;
  get mains() {
    return this._courses.mains;
  set mains(mainIn) {
    this._courses.mains = mainIn;
  get desserts() {
    return this._courses.desserts;
  set desserts(dessertsIn){
    this._courses.desserts = dessertsIn;
  get courses() {
      appetizers: this.appetixers,
      mains: this.mains,
      desserts: this.desserts
  addDishToCourse(courseName, dishName, dishPrice){
    const dish = {
      name: dishName,
      price: dishPrice
    const dishes = this._courses[courseName];
    const randomIndex = Math.floor(Math.random() * dishes.length);
    return dishes[randomIndex];
    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 meal is ${appetizer.name}, ${main.name}, ${dessert.name} and your total price is ${totalPrice}.`

menu.addDishToCourse('appetizers', 'prawn cocktail', 5.26);
menu.addDishToCourse('appetizers', 'wings', 6.25);
menu.addDishToCourse('appetizers', 'gralic bread', 4.50);

menu.addDishToCourse('main', 'steak', 11.60);
menu.addDishToCourse('main', 'burger', 9.60);
menu.addDishToCourse('main', 'pizza', 10.60);

menu.addDishToCourse('dessert', 'ice cream', 3.50);
menu.addDishToCourse('dessert', 'cake', 4.50);
menu.addDishToCourse('dessert', 'brownie', 6.50);

const meal = menu.generateRandomMeal();

I am getting the following error.


TypeError: Cannot read property ‘push’ of undefined
at Object.addDishToCourse (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:37:28)
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:57:6)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

.push() is an array method, and as such should be called on an array

If we add a .log() call to your function:

  addDishToCourse(courseName, dishName, dishPrice){
    const dish = {
      name: dishName,
      price: dishPrice

we can see you try to use 'main' as key for your courses object, but courses does not have this property. Which results in undefined

.push() method can not be called on undefined

Thank you for your advice. I thought the error was with this addDishToCourse method and was trying to debug in the wrong place, which is why I ended up using an object and not an array in the .push() method.

After using .log(), I saw that I had entered the string wrong at the end of the code, so thank you. I will try using console log next time I have an error that I am causing and see what is being pushed out.

1 Like

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