Meal maker error

Hi everyone, i’m new here, and i got stack at meal maker project.
this is ny error
0
0
0
/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:53
const totalPrice=appetizer.price+main.price+dessert.price;
^

TypeError: Cannot read property ‘price’ of undefined
at Object.generateRandomMeal (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:53:33)
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:72:15)
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)
and this 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(data){

this._courses.appetizers=data;

},

set mains(dat){

this._courses.mains=dat;

},

set desserts(da){

this._courses.desserts=da;

},

get courses(){

return {

  appetizers: this.appetizers,

  mains: this.mains,

  desserts: this.desserts,

};

},

addDishToCourses(couresName, 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);

   console.log(dishes, randomIndex);

  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 `you ate ${appetizer.name} ${main.name} ${dessert.name} you need to pay $${totalPrice}`;

} 

};

menu.addDishToCourses(‘appetizers’,‘soup’,5.00);

menu.addDishToCourses(‘main’,‘meat’,10.00);

menu.addDishToCourses(‘desserts’,‘icecream’,4.00);

menu.addDishToCourses(‘appetizers’,‘salad’,4.50);

menu.addDishToCourses(‘main’,‘chicken’,11.00);

menu.addDishToCourses(‘desserts’,‘cream’,4.00);

menu.addDishToCourses(‘appetizers’,‘borekas’,4.50);

menu.addDishToCourses(‘main’,‘mashrums with rice’,10.00);

menu.addDishToCourses(‘desserts’,‘bolero’,4.00);

let meal=menu.generateRandomMeal();

console.log(meal);
I loged ‘dishes’, and its a ‘0’.
what am i spose to do?

I see a problem in the addDishToCourses function:

You have a return that does not belong there.

The return ends the function before it can push the dish to the appropriate array in ._courses
so there’s nothing in those arrays when you try to use stuff in them later.

Also, there’s a spelling error at the beginning of that: couresName

Thank you for your answer,
so, where do i put the 'return?

return is not needed in that function at all

The purpose of that function is to change something in an object, not to give you a value.

now i have another problem:
/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:51
menu.addDishToCourses(‘appetizers’,‘soup’,5.5);
^

TypeError: menu.addDishToCourses is not a function
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:51:6)
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)
at bootstrap_node.js:542:3

my code:
const menu={

_courses:{

appetizers:[],

mains:[],

desserts:[],

},

get appetizers(){

return this._courses.appetizers;

},

set appetizers(appetizers){

this._courses.appetizers=appetizers;

},

get mains(){

return this._courses.mains;

},

set mains(main){

this._courses.mains=mains;

},

get desserts(){

return this._courses.desserts;

},

set desserts(desserts){

this._courses.desserts=desserts;

},

get courses(){

return this._courses;

},

addDishToCourse: function (courseName,dishName,dishPrice){

const dish={

  name:dishName,

  price:dishPrice,

};



this.courses[coursName].push(dish);

},

getRandomDishFromCourse(courseName){

const dishes = this._courses[courseName];

const randomIndex = Math.floor(Math.random()*dishes.length);

 console.log(dishes);

dishes[randomIndex];

},

generateRandomMeal(){

let appetizer= this.getRandomDishFromCourse('appetizers');

let main= this.getRandomDishFromCourse('mains');

let dessert= this.getRandomDishFromCourse('desserts');

const totalPrise= appetizer.price + main.price +dessert.price;

return `you ate ${appetizer} ${main} ${dessert} it cost you $${ totalPrise}.`; 

},

};

menu.addDishToCourses(‘appetizers’,‘soup’,5.5);

menu.addDishToCourses(‘main’,‘meat’,10.00);

menu.addDishToCourses(‘dessert’,‘icecream’,4.00);

menu.addDishToCourses(‘appetizers’,‘salad’,4.5);

menu.addDishToCourses(‘main’,‘chicken’,11.00);

menu.addDishToCourses(‘dessert’,‘cream’,4.00);

menu.addDishToCourses(‘appetizers’,‘borekas’,4.5);

menu.addDishToCourses(‘main’,‘mashrums with rice’,10.00);

menu.addDishToCourses(‘dessert’,‘bolero’,4.00);

let meal=menu.generateRandomMeal();

console.log(meal);

You have a lot of spelling errors. Also, you’re also missing a return.

The getRandomDishFromCourse function is missing a return.

For example,
You use mains and desserts here:

const menu={
  _courses:{
    appetizers:[],
    mains:[],
    desserts:[],
  },

and addDishToCourse here:

addDishToCourse: function (courseName,dishName,dishPrice){  

but you spelled that same thing main and dessert and addDishToCourses later:

menu.addDishToCourses('appetizers','soup',5.5);
menu.addDishToCourses('main','meat',10.00);
menu.addDishToCourses('dessert','icecream',4.00);

And in the addDishToCourse function, courseName is not the same as coursName

  addDishToCourse: function (courseName,dishName,dishPrice){
    const dish={
      name: dishName,
      price: dishPrice,
    };
    this.courses[coursName].push(dish);
  },

And here:

  set mains(main){
    this._courses.mains=mains;
  },

main and mains don’t match.

Also, in the generateRandomMeal function;
on this line:

    return `you ate ${appetizer} ${main} ${dessert} it cost you ${ totalPrise}.`; 

there’s an issue;
appetizer should be appetizer.name (because appetizer is an object, and appetizer.name is a string);
and similarly for the other ones.

code from previous post
const menu={
  _courses:{
    appetizers:[],
    mains:[],
    desserts:[],
  },

  get appetizers(){
    return this._courses.appetizers;
  },
  set appetizers(appetizers){
    this._courses.appetizers=appetizers;
  },

  get mains(){
    return this._courses.mains;
  },
  set mains(main){
    this._courses.mains=mains;
  },

  get desserts(){
    return this._courses.desserts;
  },
  set desserts(desserts){
    this._courses.desserts=desserts;
  },

  get courses(){
    return this._courses;
  },

  addDishToCourse: function (courseName,dishName,dishPrice){
    const dish={
      name: dishName,
      price: dishPrice,
    };
    this.courses[coursName].push(dish);
  },

  getRandomDishFromCourse(courseName){
    const dishes = this._courses[courseName];
    const randomIndex = Math.floor(Math.random()*dishes.length);
    console.log(dishes);
    dishes[randomIndex]; 
  },

  generateRandomMeal(){
    let appetizer= this.getRandomDishFromCourse('appetizers');
    let main= this.getRandomDishFromCourse('mains');
    let dessert= this.getRandomDishFromCourse('desserts');
    const totalPrise= appetizer.price + main.price +dessert.price;
    return `you ate ${appetizer} ${main} ${dessert} it cost you ${ totalPrise}.`; 
  },
};

menu.addDishToCourses('appetizers','soup',5.5);
menu.addDishToCourses('main','meat',10.00);
menu.addDishToCourses('dessert','icecream',4.00);
menu.addDishToCourses('appetizers','salad',4.5);
menu.addDishToCourses('main','chicken',11.00);
menu.addDishToCourses('dessert','cream',4.00);
menu.addDishToCourses('appetizers','borekas',4.5);
menu.addDishToCourses('main','mashrums with rice',10.00);
menu.addDishToCourses('dessert','bolero',4.00);

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

Thank’s a lot,
i fiixed what you told me to but its still not worrking.
i feel so stuped!!

TypeError: Cannot read property ‘push’ of undefined

const menu={

_courses:{

appetizers:[],

mains:[],

desserts:[],

},

get appetizers(){

return this._courses.appetizers;

},

set appetizers(appetizers){

this._courses.appetizers=appetizers;

},

get mains(){

return this._courses.mains;

},

set mains(mains){

this._courses.mains=mains;

},

get desserts(){

return this._courses.desserts;

},

set desserts(desserts){

this._courses.desserts=desserts;

},

get courses(){

return this._courses;

},

addDishToCourses(courseName,dishName,dishPrice){

const dish = {

  name:dishName,

  price:dishPrice,

};

this._courses[courseName].push(dish);

},

getRandomDishFromCourse(courseName){

const dishes = this._courses[courseName];

const 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 `you ate ${appetizer.name} ${main.name} ${dessert.name} it cost you $${ totalPrice}.`; 

},

};

menu.addDishToCourses(‘appetizer’,‘soup’,5.5);

menu.addDishToCourses(‘main’,‘meat’,10.00);

menu.addDishToCourses(‘dessert’,‘icecream’,4.00);

menu.addDishToCourses(‘appetizers’,‘salad’,4.5);

menu.addDishToCourses(‘main’,‘chicken’,11.00);

menu.addDishToCourses(‘dessert’,‘cream’,4.00);

menu.addDishToCourses(‘appetizer’,‘borekas’,4.5);

menu.addDishToCourses(‘main’,‘mashrums with rice’,10.00);

menu.addDishToCourses(‘dessert’,‘bolero’,4.00);

let meal=menu.generateRandomMeal();

console.log(meal);

Check your spelling for mains and desserts.

That property is spelled mains here:

  _courses:{
    appetizers:[],
    mains:[],
    desserts:[],

but its spelled main here:

menu.addDishToCourses('appetizers','soup',5.5);
menu.addDishToCourses('main','meat',10.00);
menu.addDishToCourses('dessert','icecream',4.00);
menu.addDishToCourses('appetizers','salad',4.5);
menu.addDishToCourses('main','chicken',11.00);
menu.addDishToCourses('dessert','cream',4.00);
menu.addDishToCourses('appetizers','borekas',4.5);
menu.addDishToCourses('main','mashrums with rice',10.00);
menu.addDishToCourses('dessert','bolero',4.00);

And similarly for desserts and dessert.

you are the best!! :smiley: