Learn object; error line 37 : this._courses[courseName].push(dish)

Hello I have been struggling with this for the last couple of days.
I found many items on this subject of object error line 37 but also found that there could be many reason it to be caused, yet i could not make what it is that I am doing wrong.
After wondering if I might not be smart enough to learn coding, I actually experienced some clues that its not just me in this excercise; a vamp up excercise originally not designed to teach getters and setters.
In this excercise I really needed all the get help possibilities.
I think i kind of get the concept of getters (gets the keys of an object) and setters (sets or alters the keys).
But i really would like to see this script work, what is it what I am not seeing?
And I was trying to use the debugger in VS but also that I did not get to work.
hope to get some feedback.

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(appetizer) {
      this._courses.appetizers = appetizer;
    },
    set mains(main) {
      this._courses.mains = main;
    },
        set desserts(dessert) {
      this._courses.desserts = dessert;
    },  
  get courses () { 
  return {
      appetizers: this.appetizers, 
      mains: this.mains,
      desserts: this.desserts,
  };
  },
  addDishToCourse (courseName, dishName, dishPrice) {
  const dish = {
      name: dishName,
      price: dishPrice, 
    };
    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 meal is ${appetizer.name}, ${meal.name} and ${dessert.name} and the total price is ${totalPrice} `;
    }
  
  }; 
  menu.addDishToCourse('appetizers', 'shrimp cocktail', 5);
  menu.addDishToCourse('appetizers', 'fish soup', 6);
  menu.addDishToCourse('appetizers', '6 oysters', 15);
  menu.addDishToCourse('appetizers', 'sumac unions & garlic bread', 7,50);
  menu.addDishToCourse('meals', 'beef cheecks', 20);
  menu.addDishToCourse('meals', 'korean ramen 1', 15);
  menu.addDishToCourse('meals', 'sichuan ramen', 12);
  menu.addDishToCourse('meals', 'japanese ramen', 13.50);
  menu.addDishToCourse('desserts', 'banana split', 6);
  menu.addDishToCourse('desserts', 'cheese platter', 12);
  menu.addDishToCourse('desserts', 'heavenly mud', 8);
  menu.addDishToCourse('desserts', 'coffee deluxe', 6);
  menu.addDishToCourse('desserts', 'fresh fruit', 7);
  menu.addDishToCourse('desserts', 'chocolate mousse', 6);

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


  

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

Hello, and welcome to the forums!

You aren’t alone with your experience during this exercise. Many have struggled with it because of there being so much going on, plural vs singular names, getters and setters that don’t get used in the rest of the code, etc.

Here are the debugging steps that I took:

Since it fails on line 37, which is in the addDishToCourse() method, I added a call to console.log() at the beginning of the code in that method to see if it will give me a clue. (You can format it however you’d like)

console.log('addDishToCourse called', courseName, dishName, dishPrice);

The code still errored out, but it produced some output:

addDishToCourse called appetizers shrimp cocktail 5
addDishToCourse called appetizers fish soup 6
addDishToCourse called appetizers 6 oysters 15
addDishToCourse called appetizers sumac unions & garlic bread 7
addDishToCourse called meals beef cheecks 20

The call that triggered the error was the first one with meals as the courseName. This is enough of a clue to go back over and double check the course names. In this case, it should be mains instead of meals because that’s what the property is called in this._courses .

After correcting those calls with the proper courseName, the output properly shows all the calls to menu.addDishToCourse() but now there is a new error somewhere else.

I’ll let you tackle that next error, but below is more help if you need it.

Click for info about the next error

The error is in the getRandomMeal() method. If the error doesn’t tell you what line specifically, you could use more logging to help pinpoint it. Other times you’ll get a more direct error such as: TypeError cannot read property name of undefined, which often indicates attempting to access an incorrectly named variable.

In this case, look really closely at the variable names in your return statement. Similar to the first error, main and meal were mixed up.

2 Likes

thanks! i did not get console.log(‘addDishToCourse called’, courseName, dishName, dishPrice); to work in my document, but was able to find the 2nd error.
Could you explain me where you put the console.log code to get the clues.
I think if I understand this concept, I will be much less dependent on others solving problems caused by my own inconsistencies.
Namaste!

Sure, I added the console.log() call here:

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

The idea was to add a log at the start of the method that was causing an error. If we had started with just a console.log('addDishToCourse called'); without including the values of the arguments, it still would have been a clue because we’d see that more than one printed before the error. That would mean that the function wasn’t failing every time. Adding the other values in the log allowed for more information. It could have been formatted any way you wanted, of course, to make it clearer.

You’ll find adding console.log() calls to different parts can give valuable information about where the flow is being interrupted. Commenting out blocks of code to check for different results can help too.

Good job finishing the project