I'm taking the Codecademy Javascript course. I'm tasked with making a menu. I just learned about objects and I'm still a little fuzzy on the distinction between methods and properties right now. I've been following along to their video trying to debug my code but as far as I can tell it should function the same, if not be exactly the same.
const menu = {
_courses: {
appetizers: [],
mains: [],
desserts: [],
},
get appetizers() {
return this._courses.appetizers;
},
set appetizers(appetizers) {
this._courses.appetizers = appetizers;
},
get mains() {
return this._courses.mains;
},
set mains(mains) {
this._courses.mains = mains;
},
get desserts() {
return this._courses.desserts;
},
set desserts(desserts) {
this._courses.desserts = desserts;
},
get courses() {
return {
appetizers: this.appetizers,
mains: this.mains,
desserts: this.desserts,
};
},
getRandomDishFromCourse(courseName) {
const dishes = this._courses[courseName];
const randomIndex = Math.floor(Math.random() * dishes.length);
},
addDishToCourse(courseName, dishName, dishPrice) {
const dish = {
name: dishName,
price: dishPrice,
};
this._courses[courseName].push(dish);
},
generateRandomMeal() {
const appetizer = this.getRandomDishFromCourse('appetizers');
const mains = this.getRandomDishFromCourse('mains');
const desserts = this.getRandomDishFromCourse('desserts');
const totalPrice = appetizers.price + mains.price + desserts.price;
return `Your meal is ${appetizers.name}, ${mains.name}, ${desserts.name}. The total price is ${totalPrice}.`;
}
};
menu.addDishToCourse('appetizers', 'Caesar Salad', 9);
menu.addDishToCourse('appetizers', 'Octopus', 18);
menu.addDishToCourse('appetizers', 'Scallop and Lobster Stuffed Crepe', 22);
menu.addDishToCourse('mains', 'Berkshire Porkchop', 45);
menu.addDishToCourse('mains', 'Filet Mignon', 43);
menu.addDishToCourse('mains', 'Spicy Lobster Pasta', 47);
menu.addDishToCourse('desserts', 'Deconstructed Mud Pie', 18);
menu.addDishToCourse('desserts', 'Creme Brulee', 14);
menu.addDishToCourse('desserts', 'Cheesecake', 18);
const meal = menu.generateRandomMeal();
console.log(meal);
The console is returning "ReferenceError: appetizers is not defined" at the 48th line when I'm calculating totalPrice.
As a secondary question, do the positions of the getter and setter functions matter? Thanks in advance.
Your variable name is appetizer
const appetizer = this.getRandomDishFromCourse('appetizers');
but you using appetizers
appetizers.price