Help with Menu Maker Project - setters

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

I’m struggling with this one - I get the suggested solution but I am trying to create the function .addDishToCourse() using setters created earlier…
My code looks like this:`

const menu = { 
  _courses: {
    appetizers: [],
    mains: [],  
    desserts: [],

  },    
//Getter methods
  get appetizers() {
    return this._courses.appetizers;

  },

  get mains() {
    return this._courses.mains;
  },

  get desserts() {
    return this._courses.mains;
  },

//setters
  set appetizers(appetizerIn) {
    this._courses.appetizers.push(appetizerIn);

  },

  set mains(mainIn) {
    this._courses.mains.push(mainIn);
  },

  set desserts(dessertIn) {
    this._courses.desserts.push(dessertIn);
  },

  get courses() {
    return {
      appetizers: this.appetizers,
      mains: this.mains,
      desserts: this.desserts,
    }
  },

  addDishToCourse(courseName, dishName, dishPrice) {
    const dish = {
      name: this.dishName,
      price: this.dishPrice,
    };
    //return this._courses[courseName].push(dish);
    menu.[courseName] = {dishName, dishPrice};



  },

  //getRandomDishFromCourse(courseName) {

  };

  menu.addDishToCourse('appetizers', 'salad', 30.00);

How do I call the setter taking the argument from the addDishToCourse() function? I want to call the setter for appetizers if courseName is appetizer and so forth. I could write an if-else statement but that seems clumsy. I thought that I’d be able to do menu.[courseName] = {dishName, dishPrice} but this throws an error. You’ll see I changed the suggested format of the getter to push the value into the array.

according to MDN:

The set syntax binds an object property to a function to be called when there is an attempt to set that property.

in other words, when we try to assign the property (assuming property has setter):

myObj.someProp = 'new value'

the function bound to the property will be called

there are two ways to access a property, the dot notation (like I demonstrated above with someProp) or the associative array notation:

myObj[someKey]

you seem to want to combine both of these notations, that doesn’t work.

Which notation should you use?

1 Like