Javascript Syntax Objects: Meal Maker program - .Push is not pushing object to array

In the following code , I am calling .push to push the dish object onto the array, but when I console log the array after calling .push , the array is still empty… anyone have advice on what to do next?

addDishToCourse(courseName, dishName, dishPrice){

    const dish = {

      dishName,

      dishPrice

    };

    console.log(dish);

    this._courses[courseName].push(dish);  

    console.log('Contents of array after push: ' + menu._courses[courseName]);

  },

The following is the output I am getting:

{ dishName: 'Steak and Lobster', dishPrice: 40 }
Contents of array after push:

Here is the link to the project:
https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-ii/modules/fecp-learn-javascript-syntax-objects/projects/meal-maker

Hi @erickcabanban5270797
it’s not said that your array is still empty after pushing the items. You just can’t access your array with the console that way.
See how you addressed the object here:

and then – still inside that function – here:

Consider that you’re inside the menu object.

Hi @mirja_t !

How would I check to see if there is something inside of the array? Because I even tried to console log the index:

this._courses[courseName][0]

and I am getting ‘undefined’

You should be able to log the array with console.log(this._courses[courseName]). In the code above you tried to log it with menu instead of this.
If there is no output still, then maybe there is something wrong with the argument in your function call. Could you post all of your code, if that’s the case?

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    deserts: []
  },
  get appetizers(){
    return this.appetizers
  },
  get mains(){
    return this.mains
  },
  get deserts(){
    return this.deserts
  },
  get _courses(){
    return {appetizers: [], mains: [], deserts: []}  
  },
  set appetizers(appetizerIn){
    this.appetizers = appetizerIn;
  },
  set mains(mainIn){
    this.mains = mainIn;
  },
  set deserts(desertIn){
    this.deserts = desertIn;
  },
  addDishToCourse(courseName, dishName, dishPrice){
    const dish = {
      name: dishName,
      price: dishPrice
    };

    this._courses[courseName].push(dish);  
    // Prints [] output
    console.log(this._courses[courseName]);
 
  },
  getRandomDishFromCourse(courseName){
    let dishes = this._courses[courseName]; 
    // Prints Undefined
    console.log(this._courses[courseName][0]);
    return this._courses[courseName][1];
  },
  generateRandomMeal(){
    const mains = menu.getRandomDishFromCourse('mains');
  }
}

menu.addDishToCourse('mains', 'Steak and Lobster', 40);

const meal = menu.generateRandomMeal();

You have to remove the underscore from the getter. Otherwise you have a duplicate key in your menu object. I think your code is running fine then.

What?? How does that work if I never called the courses() method?

Do getters work automatically ? What is this concept called?

Your getter doesn’t work as expected, I think, it just returns empty arrays. The misspelling caused a syntax error which spoiled all your code.
This would be a getter that would make more sense:

get courses(){
    return this._courses;  
  }

That’s how you could call it (outside the menu object), it does what you tried to achieve with the console inside your addDishToCourse method:

console.log(menu.courses);

I mean do getters work automatically? I didn’t specifically call courses() in the object

I created the method, but I didn’t call it

I’m not sure I get what you mean by ‘work automatically’.
You didn’t call it, so it didn’t do anything, it just crashed your code due to the misspelling.

How did it crash my code though if I didn’t call it?

The way I think it works is if like I create a method but don’t use it , then my code will work just fine

It’s inside your menu object. Any syntax error inside an object, function or whatever crashes your code, at least inside the current scope. Whether you use it or not.

Ok I get a little now, i need to look more into it… thanks mirja

Edit: O wait okay i see it noww… there were 2 _courses()… so I was calling the second one and just returning empty arrays

1 Like

Yes, you’re right. I just confirmed that Javacript only throws a syntax error for duplicate keys when run in strict mode. Sorry.

1 Like