Meal Maker Troubleshoot


#1

Hey guys, my first post here. I’ve had a pretty smooth time so far doing the little code projects but this one has stumped me. I watched the instructional video a few dozen times and can’t seem to figure out where exactly I went wrong, so I figured I’d ask for a little help!
Here’s my code:

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(appIn) {
    this._courses.appetizers = appIn;
  },
  set mains(mainIn) {
    this._courses.mains = mainIn;
  },
  set desserts(dessertIn) {
    this._courses.desserts = dessertIn;
  },
  get courses() {
    return {
      appetizers: this.appetizers,
      mains: this.mains,
      desserts: this.desserts
    }
  },
  addDishToCourse(courseName, dishName, dishPrice) {
    let dish = {
      name: dishName,
      price: dishPrice,
    };
    return this._courses[courseName].push(dish);
  },
  getRandomDishFromCourse(courseName) {
    let dishes = this._courses[courseName];
  	let 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('desserts');
    const totalPrice = appetizer.price + main.price + dessert.price;
    return `Appetizer is ${appetizer.name}, main course is ${main.name} and dessert is ${dessert.name}. Total price is $${totalPrice}`;
  }
};

menu.addDishToCourse('appetizers', 'Caesar Salad', 7.50);
menu.addDishToCourse('appetizers', 'Escargot', 10.00);
menu.addDishToCourse('appetizers', 'Calamari', 12.50);

menu.addDishToCourse('mains', 'Bison Burger', 17.50);
menu.addDishToCourse('mains', 'Grilled Salmon', 25.00);
menu.addDishToCourse('mains', 'Steak Frites', 30.00);

menu.addDishToCourse('desserts', 'NY Cheesecake', 12.50);
menu.addDishToCourse('desserts', 'Chocolate Ganache', 15.00);
menu.addDishToCourse('desserts', 'Tarte au Caramel', 5.00);

let meal = menu.generateRandomMeal();

console.log(meal);

And I’m receiving the error:

TypeError: Cannot read property ‘price’ of undefined
at Object.generateRandomMeal

Thanks in advance to anyone who helps me solve this! :pray:


#3

Found it!!!

You arent calling .random function from Math correctly :wink: You are missing the ().

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

#4

Took me 30 mins to spot the difference between my code and yours… :stuck_out_tongue:


#5

Thank you so much!!! Ugh what a silly mistake, I’m so glad you found that. Now I can keep the rest of my hair :stuck_out_tongue_closed_eyes: