Object exercise

Hello, currently facing a an issue in the “meal Maker” project. Whenever trying to add a new “dish” to “_courses”, it keeps saying .push is not a function, from which I understand it thinks i’m trying to push something which isn’t an array, but everything contained with “_courses” is an array.

let menu= {
  _courses:{
    appetizers:[],
    mains:[],
    desserts:[]
  },
  addDishToCourse(courseName,dishName,dishPrice){
    const dish = {
      name:dishName,
      price:dishPrice,
    };
    this._courses[courseName].push(dish);
  },
}
menu.addDishToCourse("desserts","cake","£20")
console.log(menu._courses)

edit also tried menu.addDishToCourse(desserts,"cake","£20")
but this returned as desserts undefined
Thanks,
Soup

Is this the full code you are using and getting the return of undefined?

I have used your code and am getting the array returned.

{ appetizers: [],
  mains: [],
  desserts: [ { name: 'cake', price: '£20' } ] }

Welp found the issue, in the getter I was reassigning them away from arrays! when i supposed to be making the getter get whatever was inside it! (not just assigning filler values)

let menu= {
  _courses:{
    appetizers:[],
    mains:[],
    desserts:[]
  },
  addDishToCourse(courseName,dishName,dishPrice){
    const dish = {
      name:dishName,
      price:dishPrice,
    };
    this._courses[courseName].push(dish);
  },
  
  get _courses(){
    return{ 
      appetizers: "apple",
      mains:"pizza",
      desserts:"chocolate",
    }
  },

  get appetizers(){},
  set appetizers(appetizerIn){
    
  },
  get mains(){},
  set mains(mainsIn){
    
  },
  get desserts(){},
  set desserts(dessersIn){
    
  },
}
console.log(menu._courses)

menu.addDishToCourse("desserts","cake",20)
console.log(menu._courses)

so it should be

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