Meal Maker

Hello I got always this error but I don’t know why? Thank you!

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:37
    return this._courses[courseName].push(dish);
                                    ^

TypeError: Cannot read property 'push' of undefined
    at Object.addDishToCourse (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:37:37)
    at Object.<anonymous> (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:61:7)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)
    at startup (bootstrap_node.js:151:9)

Here is my code

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() { /* only this is also ok    return _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];
},
generateRandomMeal(){
    const appetizers= this.getRandomDishFromCourse('appetizers');
    const mains = this.getRandomDishFromCourse('mains');
    const desserts = this.getRandomDishFromCourse('desserts');  
    const totalPrice = appetizer.price + main.name + dessert.name;
    return `Your meal is ${appetizer.name}, ${main.name }, and ${dessert.name},  and  the total 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', 10.25);
 menu.addDishToCourse('mains','salmon', 7.75);
 menu.addDishToCourse('mains','tofu', 1.20);

 menu.addDishToCourse('dessert','ice-cream', 3.00);
 menu.addDishToCourse('dessert','cofee', 3.00);
 menu.addDishToCourse('dessert','cake', 3.25);

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

here:

 menu.addDishToCourse('dessert','ice-cream', 3.00);

you try to add ice-cream to dessert, except that the property is named desserts, so the string here should be desserts as well.

3 Likes

Thank you. There were some same problem inside the code but this is work now

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() { /* only this is also ok    return _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];
},
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 total 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', 10.25);
 menu.addDishToCourse('mains','salmon', 7.75);
 menu.addDishToCourse('mains','tofu', 1.20);

 menu.addDishToCourse('desserts','ice-cream', 3.00);
 menu.addDishToCourse('desserts','cofee', 3.00);
 menu.addDishToCourse('desserts','cake', 3.25);

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