Meal Maker (Advanced Objects): addDishToCourse method not working

Hoping someone can help me out with this!
https://www.codecademy.com/courses/introduction-to-javascript/projects/meal-maker

I’m not getting any errors on my script, but it does not look like my ‘addDishToCourse’ method is working.

It is supposed to add dishes to my menu so I can later on randomly select dishes from my menu, but when I print out my random meal, it is pulling all blanks. Code and output are below:

const menu = {
  _courses: {
    appetizers: [ ],
    mains: [ ],
    desserts: []
  },
  
  get appetizers() {
    return this._courses.appetizers
  },
  set appetizers(appetizersIn) {
    this._courses.appetizers = appetizersIn;
  },
  get mains() {
    return this._courses.mains
  },
  set mains(mainsIn) {
    this._courses.mains = mainsIn;
  },
  get desserts() {
    return this._courses.desserts
  },
  set desserts(dessertsIn) {
    this._courses.desserts = dessertsIn;
  },
  
  get courses() {
    return {
      appetizers: this._courses.appetizers,
      mains: this._courses.mains,
      desserts: this._courses.desserts,
    }
  },
  
  addDishToCourse: function(courseName, dishName, dishPrice) {
    let dish = {
      name: [],
      price: []
    };
    
    this._courses[courseName].push(dish);
  },
  
  getRandomDishFromCourse: function(courseName) {
    let dishes = this._courses[courseName];
    let randomIndex = Math.floor(Math.random()*dishes.length);
    return dishes[randomIndex];
  },
  
  generateRandomMeal: function(){
    let randomAppetizer = this.getRandomDishFromCourse('appetizers');
    let randomMain = this.getRandomDishFromCourse('mains');
    let randomDessert = this.getRandomDishFromCourse('desserts');
    let totalPrice = randomAppetizer.price + randomMain.price + randomDessert.price;
    return `Your meal is ${randomAppetizer.name}, ${randomMain.name}, and ${randomDessert.name}, and the total price is $${totalPrice}`
  }
  
};

menu.addDishToCourse('desserts','creme brulee',25);
menu.addDishToCourse('desserts','panettone',15)
menu.addDishToCourse('desserts','biscotti',5);

menu.addDishToCourse('mains','sammich1',25);
menu.addDishToCourse('mains','sammich2',15)
menu.addDishToCourse('mains','sammich3',5);

menu.addDishToCourse('appetizers','app1',25);
menu.addDishToCourse('appetizers','app2',15)
menu.addDishToCourse('appetizers','app3',5);
console.log(menu)

var meal = menu.generateRandomMeal()
console.log(meal)

Output:

{ _courses: 
   { appetizers: [ [Object], [Object], [Object] ],
     mains: [ [Object], [Object], [Object] ],
     desserts: [ [Object], [Object], [Object] ] },
  appetizers: [Getter/Setter],
  mains: [Getter/Setter],
  desserts: [Getter/Setter],
  courses: [Getter],
  addDishToCourse: [Function: addDishToCourse],
  getRandomDishFromCourse: [Function: getRandomDishFromCourse],
  generateRandomMeal: [Function: generateRandomMeal] }
Your meal is , , and , and the total price is $
1 Like

Your method totally ignores some of its parameters
Imagine an add (+) function which should add two numbers but doesn’t look at what the numbers are. Probably isn’t going to work.

function add(a, b) {
  return 3
}
1 Like

addDishToCourse function is not setup properly. you assigned variable to empty array instead of values passed in the parameter. let dish = { name: [], price: [] };
I have changed to this and it runs smoothly: let dish = { name: dishName, price: dishPrice };
Hope thats helpful and lets keep coding :smile:

2 Likes

It works – we got random meals!! Thank you.

Another issue I am running into now is my totalPrice is turning into a concatenation instead of a sum.
For example, my console reads this:

Your meal is app2, sammich1, and biscotti, and the total price is $15255

But this is what totalPrice is defined as:
const totalPrice = randomAppetizer.price + randomMain.price + randomDessert.price;

1 Like

If you add three numbers that’ll do the math kind of addition, not concatenation.
So that would suggest you’re not adding three numbers together - which is something you can investigate

Hi @kmccarthy93, unfortunately I can’t replicate this error on my computer, it’s working fine here and adding up correctly. check arguments you passing into the function.