Hi everyone,
I’m working on the Meal Maker project and I’m doing Step 7. At first my code wasn’t working and then I made a change (mostly a random change/educated guess) and then my code worked. Not quite sure what the difference is.
So, if you look below, in my setter “appetizers” I have it set with this._courses.appetizers.push(appIn)
. Then, further down in my method “addDishToCourse” I push to the “mains” array with this._courses[courseName].push(dish)
. Both of these ways achieve the desired result. However, at fist, I had “set appetizers” the way that “set desserts” looks now. Meaning I had it written like this._courses[appetizers].push(appIn)
and was getting an error.
I’ve tried going back in lessons and reading some of the documentation, but I’m struggling to see why these two forms aren’t interchangeable. My instinct is telling me that it has something to do with scope, but I can’t seem to find solid footing here. Can anyone help me get pointed in the right direction?
I commented out the sections that will cause the code to throw an error.
As a secondary question, why does passing a string with this._courses[courseName].push(dish)
work for the key mains? I had assumed that I was going to be wrong on that one, and that the string “mains” would not translate (idk if that’s the right term) to the key.
const menu = {
_courses : {
appetizers : [],
mains : [],
desserts : []
},
get appetizers(){
},
set appetizers(appIn){
this._courses.appetizers.push(appIn);
},
get mains(){
},
set mains(mainIn){
},
get desserts(){
},
set desserts(dessertIn){
this._courses[desserts].push(appIn);
},
get courses(){
return {appetizers : this.appetizers, mains : this.mains, desserts : this.desserts}
},
addDishToCourse(courseName, dishName, dishPrice){
const dish = {
name : dishName,
price : dishPrice
};
if(courseName === 'appetizers'){
menu.appetizers = dish;
}else if(courseName === 'mains'){
this._courses[courseName].push(dish)
}else if(courseName === 'desserts'){
menu.desserts = dish;
}
}
};
menu.addDishToCourse('appetizers', 'Lo Mein', 12);
menu.addDishToCourse('mains', 'Kung Pao', 14);
//menu.addDishToCourse('desserts', 'Kung Pao', 14);
console.log(menu._courses.appetizers, menu._courses.mains)
//console.log(menu._courses.desserts)
Thank you!