Meal maker project -

I got this error, and I’m stumped. Help!

const totalPrice = appetizer.price + main.price + dessert.price;
^

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


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(appetizers) {

    this._courses.appetizers = appetizers;

  },

  set mains(mains) {

    this._courses.mains = 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 = Math.floor(Math.random * dishes.length);

    return dishes[randomIndex];

  },

  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 meal is ${appetizer.name}, ${main.name} and ${dessert.name} and the total price is ${totalPrice}.`;

  }

};

menu.addDishToCourse('appetizers','salad',4.00);

menu.addDishToCourse('appetizers','wings',4.00);

menu.addDishToCourse('appetizers','cheese',4.00);

menu.addDishToCourse('mains','steak',4.00);

menu.addDishToCourse('mains','beans',4.00);

menu.addDishToCourse('mains','mushrooms',4.00);

menu.addDishToCourse('desserts','ice cream',4.00);

menu.addDishToCourse('desserts','coffee',4.00);

menu.addDishToCourse('desserts','cake',4.00);

const meal = menu.generateRandomMeal();

console.log(meal);

the variable appetizers gets its value from getRandomDishFromCourse, so then there might be a problem with that method, we can use .log() to inspect the function/state of program:


  getRandomDishFromCourse(courseName) {

    const dishes = this._courses[courseName];

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

    console.log(dishes, randomIndex);
    return dishes[randomIndex];

  },

as we can see, randomIndex is NaN, which is a problem when you want to use this variable as index

error: Uncaught TypeError: Cannot read property ‘price’ of undefined

I effectively just copied the walkthrough as I don’t really understand any of this yet. The tutors version ran fine with the randomIndex as is…

Did you insert the .log() like I showed you? Do you see that randomIndex is NaN?

something goes wrong:

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

what could it? If you really don’t know you could add more logs:

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

to inspect/debug even further.

Best not to share my opinion/point of view on this matter/approach.

[Function: random] 3
[Function: random] 3
[Function: random] 3

This is what’s returned if i add a .log

And multiplying a function and a number gives NaN. What did you do wrong that the random function gives it reference instead of returning a random number?

I missed () after Math.random().

I learn best this way. I try doing as much as I can but i have a few weeks experience and I find having a working copy in front of me helps.

2 Likes

Double check and take a look at your .generateRandomMeal. Specifically totalPrice, the return and the difference between them and the previous variables defined in .generateRandomMeal.