Help me understand what’s going on here

Can someone please help me understand what’s going on here. Been driving me nuts.

It seem think the object ‘dishes’ does not have a length property. However, when I print dishes object using console.log() method, there’s indeed a length property.

Am I missing something?

Error message is

Length of dishes object is 4
/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:56
const randomIndex = Math.floor(Math.random() * dishes.length);*

TypeError: Cannot read property ‘length’ of undefined

  • at Object.getRandomDishFromCourse (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:56:58)*
  • at Object.generateRandomMeal (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:47:23)*
  • at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:74:17)*
  • at Module._compile (module.js:571:32)*
  • at Object.Module._extensions…js (module.js:580:10)*
  • at Module.load (module.js:488:32)*
  • at tryModuleLoad (module.js:447:12)*
  • at Function.Module._load (module.js:439:3)*
  • at Module.runMain (module.js:605:10)*
  • at run (bootstrap_node.js:427:7)*
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 (appetizersIn) {
    this._courses.appetizers = appetizersIn
  },
  set mains (mainsIn) {
    this._courses.mains = mainsIn
  },
  set desserts (dessertsIn) {
    this._courses.desserts = dessertsIn
  },
  get courses() {
    return {
      appetizers: this.appetizers,
      mains: this.mains,
      desserts: this.desserts
    }
  },

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



generateRandomMeal: function() {
    
    const appetizer = this.getRandomDishFromCourse('appetizers')
    const main = this.getRandomDishFromCourse('main')
    const dessert = this.getRandomDishFromCourse('dessert')
    const totalPrice = appetizer.price + main.price + dessert.price

    return `Your 3 course meal is ${appetizer.name}, ${main.name} and ${dessert.name}. The total price of said meal is ${totalPrice}`
  },

getRandomDishFromCourse: function(courseName) {
    const dishes = this._courses[courseName];
    console.log(`Length of dishes object is ${dishes.length}`)
    const randomIndex = Math.floor(Math.random() * dishes.length);
    return dishes[randomIndex]
  }

}


menu.addDishToCourse('appetizers', 'Shrimp', 15.99)
menu.addDishToCourse('appetizers', 'Fishkabob', 11.99)
menu.addDishToCourse('appetizers', 'Chicken Wings', 11.99)
menu.addDishToCourse('appetizers', 'lambchops', 19.99)
menu.addDishToCourse('mains', 'Jambalaya', 15.99)
menu.addDishToCourse('mains', 'Rice', 15.99)
menu.addDishToCourse('mains', 'pizza', 15.99)
menu.addDishToCourse('desserts', 'rice pudding', 15.99)
menu.addDishToCourse('desserts', 'ice-cream', 15.99)
menu.addDishToCourse('desserts', 'Cake', 15.99)

let meal = menu.generateRandomMeal();
console.log(meal);

You have typos here in the generateRandomMeal function

here:
this.getRandomDishFromCourse('main');
'main' should be 'mains'
and
'dessert'
should be
'desserts'

because
mains and desserts is used at the beginning here:

which is then used in the getRandomDishFromCourse function:

const dishes = this._courses[courseName];

which then affects the stuff in the generateRandomMeal function
and the getRandomDishFromCourse function

Sharp eye!

I feel like an idiot now. Thanks a ton for catching this.