My solution to Meal Maker project after a tedious debugging process!

finally, I did it, here is my solution to the project “Meal Maker” after several hours of debugging and fixing errors, may anyone could use it as a reference

const menu = {

  _courses: {

appetizers: [],

mains: [],

desserts: [], 

},

get courses() {

  return {

    appetizers: this.appetizers,

    mains: this.mains,

    desserts: this.desserts,

  };

},

get appetizers () {

return this._courses.appetizers;

},

set appetizers (appetizerIn) {

this._courses.appetizers = appetizerIn;

},

get mains () {

return this._courses.mains;

},

set mains (mainIn) {

this._courses.main = mainIn;

},

get desserts () {

return this._courses.desserts;

},

set desserts (dessertIn) {

this._courses.desserts = dessertIn;

},

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

},

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];

},

};

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

menu.addDishToCourse('appetizers', 'ceaser sause', 2);

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

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

menu.addDishToCourse('mains', 'pasta', 15.50);

menu.addDishToCourse('mains', 'pizza', 12.30);

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

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

menu.addDishToCourse('desserts', 'pudding', 8.30);

let meal = menu.generateRandomMeal();

console.log(meal);

Hi @amrmusharrafa8940871! Quite a good solution you have there. I would just make some improvements to your addDishToCourse and getRandomDishFromCourse functions. Remember that you shouldn’t call the private attributes of objects, if not what are the getters and setters for?

By these means, I would change those functions to the following:

addDishToCourse (courseName, dishName, dishPrice) {
   const dish = {
      name: dishName,
      price: dishPrice,
   };
   //Remember that 'this' is a reference to menu
   return this[courseName].push(dish); //In other words I am using menu[courseName], which calls the getter from that course
},

getRandomDishFromCourse (courseName) {
   //Same here, instead of calling the private property this._courses[courseName], I call the getter
   const dishes = this[courseName]; 
   const randomIndex = Math.floor(Math.random()*dishes.length);
   return dishes[randomIndex];
},

Also, don´t forget to comment on your code for others to understand. And using the button image when sharing code in posts for better formating.

Continue with your great job. Happy coding!

1 Like