Hi, everyone. I’m working on Meal Maker and have encountered a brick wall I can’t seem to break through.
Here’s the code I wrote up. My apologies if it looks a little messy.
let menu = {
_courses: {
appetizers: [],
mains: [],
desserts: [],
},
get appetizers() {
this._courses.appetizers;
},
set appetizers(aIn) {
this._courses.appetizers = aIn;
},
get mains() {
this._courses.mains;
},
set mains(mIn) {
this._courses.mains = mIn;
},
get desserts() {
this._courses.desserts;
},
set desserts(dIn) {
this._courses.desserts = dIn;
},
get courses() {
return {
appetizers: this.appetizers,
mains: this.mains,
desserts: this.desserts
}
},
addDishToCourse (courseName, dishName, dishPrice) {
const dish = {
dish: dishName,
price: dishPrice
};
return this._courses[courseName].push(dish);
},
getRandomDishFromCourse (courseName) {
let dishes = this._courses[courseName];
let randomIndex = Math.floor(Math.random() * dishes.length);
return dishes[randomIndex];
},
generatorRandomMeal() {
const appetizer = this.getRandomDishFromCourse('appetizers');
const main = this.getRandomDishFromCourse('mains');
const dessert = this.getRandomDishFromCourse('deserts');
const totalPrice = appetizer.price + main.price + dessert.price;
return `Your meal is ${appetizer.name}, ${main.name}, and ${dessert.name}. The price is ${totalPrice}.`;
}
};
menu.addDishToCourse('appetizers', 'cheese sticks', 4.50);
menu.addDishToCourse('appetizers', 'fried mushrooms', 2.50);
menu.addDishToCourse('appetizers', 'house salad', 1.50);
menu.addDishToCourse('mains', 'steak', 19.50);
menu.addDishToCourse('mains', 'lobster', 25.00);
menu.addDishToCourse('mains', 'blt', 10.50);
menu.addDishToCourse('desserts', 'flan', 3.50);
menu.addDishToCourse('desserts', 'cheese cake', 5.50);
menu.addDishToCourse('desserts', 'fried ice cream', 8.50);
let meal = menu.generatorRandomMeal();
console.log(meal);
The computer says there’s a TypeError on line 41, under getRandomDishFromCourse. It says the length property is undefined.
let randomIndex = Math.floor(Math.random() * dishes.length);
My biggest problem with coding is typos, but I couldn’t find anything in the code. I looked through the hints and followed the video guide but still can’t figure out what I’m doing wrong.
If anyone sees the solution or has some advice, I’d be happy to hear it. Thank you!