Meal Maker Madness


#1

Hi Js.Ninjas !

I somehow I keep getting an error in my meal maker and i have no idea why?

let menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
  },
  
  get appetizers () {
    return this._courses.appetizers;
  },
  
  get mains () {
    return this._courses.mains;
  },
  
  get dessert() {
    return this._courses.desserts;
  },
  
  set appetizers (appetizers) {
    return this._courses.appetizers = appetizers;
  },
  
  set mains (mains) {
    return this._courses.mains = mains;
  },
  
  set desserts (desserts) {
    return this._courses.desserts = desserts;
  },
  
  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) {
    var dishes = this._courses[courseName];
    var 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 `Your meal is ${appetizer.name}, ${main.name}, ${dessert.name} The price is $${totalPrice}.`;
  } 
  
}

menu.generateRandomMeal('Appetizer', 'Olives', 2.99 );
menu.generateRandomMeal('Appetizer', 'Coffee', 1.99 );
menu.generateRandomMeal('Appetizer', 'Nuts', 1.99 );

Perhaps someone can shed some light?

Luv
A.


#2

The first thing I notice is that you have not defined the get courses correctly.

get courses () {
    return {
      appetizers:this.appetizers,
      mains:this.mains,
      desserts:this.desserts,
    }
  }

Should be:

get courses () {
    return {
      appetizers: this._courses.appetizers,
      mains: this._courses.mains,
      desserts: this._courses.desserts,
    }
  }

Also you call generateRandomMeal with ‘Appetizer’ as an input value.

menu.generateRandomMeal('Appetizer', 'Olives', 2.99 );

This is a string with capital A whilst your _courses keys are written in lowercaps ‘appetizers’.
This is very important because generateRandomMeal calls the addDishToCourse function as follows:

addDishToCourse (courseName, dishName, dishPrice) {
    let dish = {
      name: dishName,
      price: dishPrice,
    };
    
    return this._courses[courseName].push(dish);
  },

If your courseName is different from the _courses keys it won’t work.


#3

Ow and a detail I just noticed… :stuck_out_tongue:

You use generateRandomMeal function to add dishes whilst you should use the
menu.addDishToCourse function to add dishes.

menu.generateRandomMeal('Appetizer', 'Olives', 2.99 );

// should be

menu.addDishToCourse('appetizers', 'Olives', 2.99);  // etc..

#4

Hi Jannes,

Oh…now it all makes sense.
Thank you so much for taking the time.

Cheers!
A.


#5

You are very welcome. Be sure to check one of my answers as the solution to your question :). Ticking the checkbox.