Meal Maker project - Cannot read property 'length' of undefined

const menu = {
_courses: {

const menu = { _courses: { appetizers: [], mains: [], desserts: [], }, get appetizers() { return this._courses.appetizers }, get mains() { return this._courses.mains }, get desserts() { return this._courses.desserts }, set appetizers(appetizers) { return this._courses.appetizers = appetizers }, set mains(mains) { return this._courses.mains = mains }, set desserts(desserts) { return this._courses.desserts = desserts }, get courses() { return { appetizers: this.appetizers, mains: this.mains, desserts: this.desserts, } }, addDishToCourse(courseName, dishName, dishPrice) { const dish = { name: dishName, price: dishPrice, } return this._courses[courseName].push(dish) }, getRandomDishFromCourse(courseName) { const dishes = this._courses[courseName] const randomIndex = Math.floor(Math.random() * dishes.length) return dishes[randomIndex] }, generateRandomMeal() { const appetizer = this.getRandomDishFromCourse('appetizer') const mains = this.getRandomDishFromCourse('mains') const desserts = this.getRandomDishFromCourse('desserts') const totalPrice = appetizer.price + mains.price + desserts.price return `Your meal is ${appetzer.name}, ${main.name} and, ${dessert.name}, and the total price is ${totalPrice}` } } menu.addDishToCourse('appetizers', 'barata', 12.00) menu.addDishToCourse('appetizers', 'bread basket', 13.00) menu.addDishToCourse('appetizers', 'carpacio', 14.00) menu.addDishToCourse('mains', 'pizza', 23.00) menu.addDishToCourse('mains', 'pasta', 25.00) menu.addDishToCourse('mains', 'oysters', 89.00) menu.addDishToCourse('appetizers', 'creme bruele', 12.00) menu.addDishToCourse('appetizers', 'keylime pie', 13.00) menu.addDishToCourse('appetizers', 'ice cream', 14.99) const meal = menu.generateRandomMeal() console.log(meal)
appetizers: [],
mains: [],
desserts: [],

},
get appetizers() {
return this._courses.appetizers
},
get mains() {
return this._courses.mains
},
get desserts() {
return this._courses.desserts
},
set appetizers(appetizers) {
return this._courses.appetizers = appetizers
},
set mains(mains) {
return this._courses.mains = mains
},
set desserts(desserts) {
return this._courses.desserts = desserts
},
get courses() {
return {
appetizers: this.appetizers,
mains: this.mains,
desserts: this.desserts,
}
},
addDishToCourse(courseName, dishName, dishPrice) {
const dish = {
name: dishName,
price: dishPrice,
}
return this._courses[courseName].push(dish)
},
getRandomDishFromCourse(courseName) {
const dishes = this._courses[courseName]
const randomIndex = Math.floor(Math.random() * dishes.length)
return dishes[randomIndex]
},
generateRandomMeal() {
const appetizer = this.getRandomDishFromCourse(‘appetizer’)
const mains = this.getRandomDishFromCourse(‘mains’)
const desserts = this.getRandomDishFromCourse(‘desserts’)
const totalPrice = appetizer.price + mains.price + desserts.price
return Your meal is ${appetzer.name}, ${main.name} and, ${dessert.name}, and the total price is ${totalPrice}
}
}

menu.addDishToCourse(‘appetizers’, ‘barata’, 12.00)
menu.addDishToCourse(‘appetizers’, ‘bread basket’, 13.00)
menu.addDishToCourse(‘appetizers’, ‘carpacio’, 14.00)

menu.addDishToCourse(‘mains’, ‘pizza’, 23.00)
menu.addDishToCourse(‘mains’, ‘pasta’, 25.00)
menu.addDishToCourse(‘mains’, ‘oysters’, 89.00)

menu.addDishToCourse(‘appetizers’, ‘creme bruele’, 12.00)
menu.addDishToCourse(‘appetizers’, ‘keylime pie’, 13.00)
menu.addDishToCourse(‘appetizers’, ‘ice cream’, 14.99)

const meal = menu.generateRandomMeal()
console.log(meal)

Veery small typo: the property in _courses is “appetizers”. You’re attempting to get a property called appetizer.

const menu = { _courses: { appetizers: [], mains: [], desserts: [], }, get appetizers() { return this._courses.appetizers }, get mains() { return this._courses.mains }, get desserts() { return this._courses.desserts }, set appetizers(appetizers) { return this._courses.appetizers = appetizers }, set mains(mains) { return this._courses.mains = mains }, set desserts(desserts) { return this._courses.desserts = desserts }, get courses() { return { appetizers: this.appetizers, mains: this.mains, desserts: this.desserts, } }, addDishToCourse(courseName, dishName, dishPrice) { const dish = { name: dishName, price: dishPrice, } return this._courses[courseName].push(dish) }, getRandomDishFromCourse(courseName) { const dishes = this._courses[courseName] const randomIndex = Math.floor(Math.random() * dishes.length) return dishes[randomIndex] }, generateRandomMeal() { const appetizer = this.getRandomDishFromCourse('appetizers') const main = this.getRandomDishFromCourse('mains') const dessert = this.getRandomDishFromCourse('desserts') const totalPrice = appetizer.price + main.price + dessert.price return `Your meal is ${appetizer.name}, ${main.name} and, ${dessert.name}, and the total price is ${totalPrice}` } } menu.addDishToCourse('appetizers', 'barata', 12.00) menu.addDishToCourse('appetizers', 'bread basket', 13.00) menu.addDishToCourse('appetizers', 'carpacio', 14.00) menu.addDishToCourse('mains', 'pizza', 23.00) menu.addDishToCourse('mains', 'pasta', 25.00) menu.addDishToCourse('mains', 'oysters', 89.00) menu.addDishToCourse('appetizers', 'creme bruele', 12.00) menu.addDishToCourse('appetizers', 'keylime pie', 13.00) menu.addDishToCourse('appetizers', 'ice cream', 14.99) const meal = menu.generateRandomMeal() console.log(meal)

I attached a codebyte. I’m getting another error now

It seems the desserts array is empty on calling the generateRandomMeal() function. That means the code is trying to acces a price property of an empty array ([]), which doesn’t exist.