First time user : Need help with code

Hello all,
My apologies for any problems with this post as it is my first time . I’m struggling with this specific project, I keep getting the "TypeError: Cannot read property ‘length’ of undefined at Object.getRandomDishFromCourse " For my code down below any help would be greatly appreciated. The link and code are down below.

const menu = { _courses: { appetizers: [], mains: [], desserts: [], }, get appetizers () { return this._courses.appetizers; }, set appetizers (appetizerIn){ appetizerIn = this._courses.appetizers; }, get mains () { return this._courses.mains; }, set mains (mainsIn){ mainsIn = this._courses.mains; }, get desserts () { return this._courses.desserts; }, set desserts (dessertsIn){ dessertsIn = this._courses.desserts; }, get courses () { return { appetizers : this._courses[' appetizers'], mains: this._courses['mains'], desserts: this._courses ['desserts'] } }, addDishToCourse (courseName,dishName,dishPrice) { if (courseName === 'appetizers' || courseName === 'mains' || courseName === 'desserts') { const dish = { name : dishName, price : dishPrice }; return this._courses[courseName].push(dish); } else { console.log(`Please enter either appetizers, mains or desserts.`); } }, getRandomDishFromCourse (courseName) { const dishes = this._courses[courseName]; const randomIndex = Math.floor(Math.random() * dishes.length); return dishes[randomIndex]; }, generateRandomMeal () { const appertizer = this.getRandomDishFromCourse(; const mains = this.getRandomDishFromCourse(; const desserts = this.getRandomDishFromCourse(; const totalPrice = mains.price + desserts.price + appetizers.price; return `Your Meal is ${}, ${}, ${} The price is ${totalPrice}.`; } }; menu.addDishToCourse('appetizers' ,'salad',1); menu.addDishToCourse('appetizers' ,'Rocket salad',2) menu.addDishToCourse('appetizers' ,'Garlic salad',3) menu.addDishToCourse('mains' ,'steak lennox',21); menu.addDishToCourse('mains' ,'chicken',13); menu.addDishToCourse('mains' ,'fish',18); menu.addDishToCourse('desserts' ,'red valvet',12); menu.addDishToCourse('desserts' ,'choclate cake',8); menu.addDishToCourse('desserts' ,'cheese cake',9); let meal = menu.generateRandomMeal();

Hello, welcome to the forums.

At first glance, I’d suggest that you take a look at what you’re passing as a parameter when calling getRandomDishFromCourse… I think you’ve confused what you’re meant to be providing there. :slight_smile:

There are also a few small typos with regards to which variables you’re using inside generateRandomMeal.

1 Like

addDishToCourse <------remove if-else statement as it is not required and it should work.

addDishToCourse(courseName, dishName, dishPrice) {
const dish = {
name: dishName,
price: dishPrice,
return this._courses[courseName].push(dish);

1 Like

Thank you for the help I’m looking at it now appreciate the time

1 Like

Thank you for the help i’m removing it now

it worked thank you soo much :sweat_smile: I was staring at the laptop for hours.

1 Like

No problem. :smiley:

Sometimes the best thing you can do after staring down a problem for what feels like ages is take a break, and come back with fresh eyes for another look. :slight_smile: