Meal maker

Hello!
I have a problem with my code.
When I log it, it returns undefined with no error :frowning:

“Your meal is undefined, undefined, and undefined, and the price is $22.”

Like this!!

Can someone help me? Thanks!

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 = {
      dishName: 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 price is $${totalPrice}.`;
  }
};

menu.addDishToCourse('appetizers','salad',4.00);
menu.addDishToCourse('appetizers','wings',4.50);
menu.addDishToCourse('appetizers','fries',5.00);

menu.addDishToCourse('mains','steak',21.00);
menu.addDishToCourse('mains','spaghetti',15.00);
menu.addDishToCourse('mains','hamburger',13.00);

menu.addDishToCourse('desserts','ice cream',5.00);
menu.addDishToCourse('desserts','tiramisu',5.00);
menu.addDishToCourse('desserts','cream ball',5.00);

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


1 Like

Hi there, welcome to the forums.

Your totalPrice variable has a value, so clearly each of your course variables - appetizer, main, dessert - have values.

The question is, though: do your dish objects get created with a name property? :wink:

If you’re really stuck… take a look at the properties of the dish object you’re creating inside of addDishToCourse()

1 Like

This is a common problem that relates to context. Examine which context the getters and setters are written in; i.e., where does the _courses object get closed?

1 Like