Working on the meal maker project in the objects portion of the javascript course. https://www.codecademy.com/courses/introduction-to-javascript/projects/meal-maker
const menu = {
_courses: {
appetizers: [],
mains: [],
desserts: [],
},
get appetizers () {
return this._courses.appetizers;
},
set appetizers(appIn){
this._courses.appetizers = appIn;
},
get mains () {
return this._courses.mains;
},
set mains (mainIn) {
this._courses.mains = mainIn;
},
get desserts () {
return this._courses.desserts;
},
set desserts (dessertIn) {
this._courses.desserts = dessertIn;
},
get _courses() {
return {
appetizers: this.appIn,
mains: this.mainIn,
desserts: this.dessertIn
}
},
addDishToCourse: function (courseName, dishName, dishPrice) {
const dish = {
name: dishName,
price: dishPrice,
};
return this._courses[courseName].push(dish);
},
getRandomDishFromCourse: function (courseName) {
const dishes = this._courses[courseName];
const index = Math.floor(Math.random() * dishes.length)
return dishes[index];
},
generateRandomMeal: function () {
let randApp = this.getRandomDishFromCourse('appetizers');
let randMain = this.getRandomDishFromCourse('mains');
let randDessert = this.getRandomDishFromCourse('desserts');
let totalPrice = randApp.price + randMain.price + randDessert.price;
return `Your meal for this evening is ${randApp.name} to start, followed by ${randMain.name}, and a lovely ${randDessert.name} to finish! This lovely meal for only ${totalPrice}.`;
},
};
menu.addDishToCourse ('appetizers', 'Medjool Dates', 8);
menu.addDishToCourse ('mains', 'Roasted Half Chicken', 25);
menu.addDishToCourse ('desserts', 'Tres Leches Cake', 9);
menu.addDishToCourse ('appetizers', 'Brown-Butter Sage Gnocchi', 12);
menu.addDishToCourse ('mains', 'Glazed Beef Short-Rib', 30);
menu.addDishToCourse ('desserts', 'Lemon Semi-Fredo', 8);
menu.addDishToCourse ('appetizers', 'Roasted Squash and Kale Salad', 7);
menu.addDishToCourse ('mains', 'Bone-In Pork Chop', 20);
menu.addDishToCourse ('desserts', 'Cheesecake', 9);
const meal = menu.generateRandomMeal();
console.log(meal);
I keep getting an error on line 38:
addDishToCourse: function (courseName, dishName, dishPrice) {
const dish = {
name: dishName,
price: dishPrice,
};
return this._courses[courseName].push(dish);
âTypeError: Cannot read property âpushâ of undefined
at Object.addDishToCourseâ
My code is identical to the guide, as far as I can tell, outside of me changing some variable names so that I dont have 8 freaking âappetizers.â Canât figure out why it thinks dish is undefined.