Meal Maker

Hi there,

I’m desperately stuck trying to get this ‘Meal Maker’ project done. I keep getting an error of ‘Cannot read property length of undefined’ at this point of the code:

const randomIndex = Math.floor(Math.random() * dishes.length);

It’s nested under the getRandomDishesFromCourse method. I’ve checked it against the solutions video, and it looks exactly the same to me. Hoping a pair of fresh eyes can clarify the problem! Thanks.

const menu = {
  get appetizers(){
    return this._courses.appetizers
  get mains(){
    return this._courses.mains
  get desserts(){
    return this._courses.desserts

  set appetizers (appetizers){
    this._courses.appetizers = appetizers;
  set mains(mains){
    this._courses.mains = mains;
  set desserts(desserts){
  get courses(){
    return {
      appetizers: this.appetizers,
      mains: this.mains,
      desserts: this.desserts};
  addDishToCourse(courseName, dishName, dishPrice){
    const dish= {
        name: courseName,
        price: dishPrice
  getRandomDishFromCourse(courseName) {
    const dishes = this._courses[courseName];
   const randomIndex = Math.floor(Math.random() * dishes.length);
   return dishes[randomIndex];
generateRandomMeal (){
    let appetizer = this.getRandomDishFromCourse('appetizers');
    let main = this.getRandomDishFromCourse('mains');
    let dessert = this.getRandomDishFromCourse('dessert');
    let totalPrice= appetizer.price + main.price + dessert.price;
    return `Your meal is ${}, ${}, ${}. The price is ${totalPrice}`;




menu.addDishToCourse('desserts','ice cream',3.75)

const meal = menu.generateRandomMeal();

Hello, @eranders, and welcome to the forums.

Rather than proof reading, or comparing your code to someone else’s code, try observing what your code actually does. You can do that by using console.log(). You might give this a try:

Thanks for your reply.

I guess I don’t really understand what’s going on with this code snippet. I thought “courseName” would be a variable I could pass through the function of getRandomDishFromCourse. So if I console.log(courseName), I get nothing. But isn’t that expected, since courseName is a variable?

Sorry for the late reply. I didn’t notice that you had replied to me. When I run your code with the console.log statements I added as shown above the output is as follows:

[ { name: ‘appetizers’, price: 5 },
{ name: ‘appetizers’, price: 4 },
{ name: ‘appetizers’, price: 4.5 } ]
[ { name: ‘mains’, price: 22 },
{ name: ‘mains’, price: 17 },
{ name: ‘mains’, price: 15 } ]

So, when courseName is assigned to ‘appetizers’, dishes is assigned to an array of objects. Same when courseName is assigned to ‘mains’, but when courseName is assigned to ‘dessert’ we see the source of your error. dishes is being assigned to undefined instead of an array of objects. undefined has no length property, so you see the error message. Where did ‘dessert’ come from? That would be here:

You passed it as the argument to your getRandomDishFromCourse() function. Is ‘dessert’ what you meant to pass?