Meal maker typeerror trouble

Trouble with meal maker, from the Objects module Javascript part 2. I have ironed out all the little errors I made however am unable to figure out why i'm being throw this error: "TypeError: Cannot read property 'price' of undefined" on line 52. Apparently the program either hasn't set the price for each dish, or I have entered numbers in the wrong format. Any help would be appreciated i'm tearing my hair out!

let menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: []
    },

  get appetizers() {
    return this._courses.appetizers
  },
  set appetizers(appetizers) {
    this._courses.appetizers = appetizers
  },
  get mains() {
    return this._courses.mains
  },
  set mains(mains) {
    this._courses.mains = mains
  },
  get desserts() {
    return this._courses.mains
  },
  set desserts(desserts) {
    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 = dishes[Math.floor(Math.random() * dishes.length)]
  },

  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 randomly chosen meal will begin with ${appetizer.name} for starters, followed by the world famous ${main.name} and topped off with our chef's specialty ${dessert.name}. We hope you enjoy your meal`
  }
}
menu.addDishToCourse('appetizers', 'Crab Soup', 3.99)
menu.addDishToCourse('appetizers', 'Olives', 1.99)
menu.addDishToCourse('appetizers', 'Bread and Humous', 2.50)
menu.addDishToCourse('mains', 'Lasagne', 11.99)
menu.addDishToCourse('mains', 'Lobster', 25.49)
menu.addDishToCourse('mains', 'Burger and Chips', 9.99)
menu.addDishToCourse('desserts', 'Lemon Tart', 3.49)
menu.addDishToCourse('desserts', 'Pancakes', 2.49)
menu.addDishToCourse('desserts', 'Trifle', 2.49)

let meal = menu.generateRandomMeal()
console.log(meal)
type or paste code here

Hello! Notice how your getRandomDishFromCourse() function doesn’t actually return anything? That means that JavaScript automatically assigns undefined as the return value for that function. Therefore, whatever you try to do based on an output from that function just leads to you trying to do something with undefined.

1 Like

Thankyou :slight_smile: So hard to see something obvious when it’s been staring you in the face for ages

1 Like