MealMaker Question

I just have a question on why something is done this way in the solution. In the setters

  set appetizers(appetizerIn){
      this._courses.appetizers = appetizerIn;
  },

Why do we set the appetizerIn to
this._courses.appetizers.

Shouldn’t the code be

this._courses.appetizers.push(appetizerIn)

since the appetizers is an empty array? The setter is never called in the code but I don’t see how this would not cause an error if it was called.

My code

Thank you,

Hello @micro4890212667 , welcome to the forums! I believe the reason for this is that you pass in to this setter function an array(?). If that’s the case, then you don’t want a nested array, so you’re just replacing the empty array with a new array. I think that’s the case, but I haven’t read the instructions…

I was wondering about the same thing when I did the exercise and just checked the instruction again:
They just say this:

> Create getter and setter methods for the `appetizers` , `mains` , and `desserts` properties

And also the hint doesn’t get more specific:

set appetizers(appetizerIn) {
 
}

So your suggestion to push to the arrays is also fine I guess.
But since the instructions also ask you to write a method that pushes dishes to the courses (.addDishToCourse()), it would do the same thing as the method does.
Maybe you might want to use the setter to replace all courses which you could do with the assignment.

@codeneutrino appetizers is indeed an array, but according to the instructions it is supposed to store multiple objects with three properties for each dish.

Ah, I see. I think your suggestion of the .addDishToCourse() method is the most reasonable.

Thanks for the response. I think I know what your saying but the property was initialized as an empty array. So wouldn’t the push method just add the object to the end of the empty array?

I think the code should look like below:

let menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts:[]
  },
  get appetizers(){
    return this._courses.appetizers;
  },
  set appetizers(appetizerIn){
      this._courses.appetizers.push(appetizerIn);
  },

but the solution has the setter as:

  set appetizers(appetizerIn){
      this._courses.appetizers =appetizerIn;
  },

Thanks! Like you said you might want to use the setter to replace all the courses which makes sense. Pushing the array also works in my tests.

menu.appetizers = {
  name: 'clams',
  price: 15.25
}

Thanks everyone for the help! Much appreciated!

1 Like