Meal maker

Hi guys :slight_smile: I cannot figure out whats wrong. I have checked every line of code with the video and the code here. I always end up with:

**The meal is undefined + undefined + undefined for NaN **
HEELP please :wink:

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];
  },
  getRandomMeal() {
    const appetizer = this.getRandomDishFromCourse('appetizers');
    const main = this.getRandomDishFromCourse('mains');
    const dessert = this.getRandomDishFromCourse('desserts')
    const totalPrice = appetizer.price + main.price + dessert.price
    return `The meal is ${appetizer.name} + ${main.name} + ${dessert.name} for ${totalPrice} `
  },
  
}
menu.addDishToCourse('appetizers', 'olives', 4.00);
menu.addDishToCourse('appetizers', 'wings', 5.30);
menu.addDishToCourse('appetizers', 'fries', 6.00);

menu.addDishToCourse('mains', 'steak', 14.50)
menu.addDishToCourse('mains', 'tofu', 12.30)
menu.addDishToCourse('mains', 'Burger', 14.10)

menu.addDishToCourse('desserts', 'ice cream', 7.00)
menu.addDishToCourse('desserts', 'pineapple', 4.00)
menu.addDishToCourse('desserts', 'coffee', 3.00)

const meal = menu.getRandomMeal();


console.log(meal)

the getRandomMeal method returns an object. This object is created in the addDishToCourse method:

{
      Name: dishName, 
      Price: dishPrice,
    }

which has two properties: Name and Price (notice the capitalize first letter). Yet here:

    const totalPrice = appetizer.price + main.price + dessert.price
    return `The meal is ${appetizer.name} + ${main.name} + ${dessert.name} for ${totalPrice} `

you use price and name (lowercase). These properties do not exists which means we get undefined instead

2 Likes

Yep welcome to coding…
I defined Name and Price with capital letters…

I literally figured it out 5 mnuten after posting this here XD

Thank you for your help… Amazing how quick!!!
I just started reading the cheatsheet a few minutes ago and one of the first paragraphs was about Accessing non-existent JavaScript properties…exactly what I had to check!

Thank you!!!