Meal Maker project, Keeps getting price is not defined

Hello everybody, I have been stuck with this error for so long now. I don’t know how to fix it. Can you help me, please? Thanks in advance.

generateRandomMeal(){
  const appetizer = this.getRandomDishFromCourse('appetizers');

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

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

let total = appetizer[price] + main[price] + dessert[price]; 

return `'You've ordered ${appetizer.name}, ${main.name} and ${dessert.name}.The total is ${total} francs CFA. We thank you for your purchase.`
 
}

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

ANd my addDishToCourse function is:

 addDishToCourse(courseName, dishName,dishPrice){

dish:{
return {
    name: dishName,
    price: dishPrice
    }
}

if(dishName === 'appetizers'){
this._courses.appetizers.push(dish);
}
else if(dishName === 'mains'){
this._courses.mains.push(dish);
}
else if(dishName === 'desserts'){
this._courses.desserts.push(dish);
}},

And the error is:

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:69
let total = appetizer[price] + main[price] + dessert[price]; 
                      ^

ReferenceError: price is not defined
    at Object.generateRandomMeal (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:69:23)
    at Object.<anonymous> (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:90:17)
    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)

You could try printing values to the console to see if what you want to happen is what is actually happening. For example:

 addDishToCourse(courseName, dishName,dishPrice){

console.log(`courseName: ${courseName} dishName: ${dishName} dishPrice: ${dishPrice}`); //see what the data passed to the function looks like

dish:{
return {
    name: dishName,
    price: dishPrice
    }
}

console.log(`dish: ${dish}`); //is the dish object created properly? let's log it, and see

if(dishName === 'appetizers'){
this._courses.appetizers.push(dish);
}
else if(dishName === 'mains'){
this._courses.mains.push(dish);
}
else if(dishName === 'desserts'){
this._courses.desserts.push(dish);
}},

I am having an undefined result.

Could you post all of your code, please?

My bad, here it is:

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

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

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

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

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

 addDishToCourse(courseName, dishName,dishPrice){

dish:{
return {
    name: dishName,
    price: dishPrice
    }
}

if(dishName === 'appetizers'){
this._courses.appetizers.push(dish);
}
else if(dishName === 'mains'){
this._courses.mains.push(dish);
}
else if(dishName === 'desserts'){
this._courses.desserts.push(dish);
}},

getRandomDishFromCourse(courseName){
  let dishes = this._courses[courseName];
  let i = Math.floor(Math.random()*dishes.length);
  return dishes[i];
},

generateRandomMeal(){
  const appetizer = this.getRandomDishFromCourse('appetizers');

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

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

let total = appetizer[price] + main[price] + dessert[price]; 

return `'You've ordered ${appetizer.name}, ${main.name} and ${dessert.name}.The total is ${total} francs CFA. We thank you for your purchase.`
 
}

//end
}

//(courseName, dishName,dishPrice

menu.addDishToCourse( 'mains', 'Thiebou Yapp', 1500);
menu.addDishToCourse('mains', 'Thiebou Dieune', 2000 );
menu.addDishToCourse('appetizers','salade', 500);
menu.addDishToCourse('mains', 'Frites et poulet', 3500);
menu.addDishToCourse('desserts', 'cake', 500);
menu.addDishToCourse('desserts', 'glace italienne', 900);
menu.addDishToCourse('appetizers','tomates et concombre', 1200);
menu.addDishToCourse('desserts','jus Bouye/Bissap', 6100);
menu.addDishToCourse('desserts', 'soft bread', 4500);

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

Let’s try inspecting your menu object after you’ve made your calls to the addDishToCourse method. If you comment out the last two lines to avoid calling the generateRandomMeal() method, and add console.log(menu);, do you see any of the added dishes?

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

Here is what I got:

{ _courses: { appetizers: [], mains: [], desserts: [] },
  appetizers: [Getter/Setter],
  mains: [Getter/Setter],
  desserts: [Getter],
  courses: [Getter],
  addDishToCourse: [Function: addDishToCourse],
  getRandomDishFromCourse: [Function: getRandomDishFromCourse],
  generateRandomMeal: [Function: generateRandomMeal] }

I forgot to create a setter for desserts.

The important part to notice here is that you have 3 empty arrays. If your addDishToCourse method was working, you should have items in your appetizers, mains and desserts arrays. So, we need to first focus on the addDishToCourse method:

My bad, I meant to put a let instead of return.

Consider the syntax used to create the menu object. You are creating a dish object.

I changed it now. I log to the console the menu object. The result is :

{ _courses: { appetizers: [], mains: [], desserts: [] },
  appetizers: [Getter/Setter],
  mains: [Getter/Setter],
  desserts: [Getter/Setter],
  courses: [Getter],
  addDishToCourse: [Function: addDishToCourse],
  getRandomDishFromCourse: [Function: getRandomDishFromCourse],
  generateRandomMeal: [Function: generateRandomMeal] }

Can I see your updated code? The next step is you have a proper dish object will be to make sure it gets pushed to the proper array.

Yes I have already used the if else if statements . My actual code is:

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

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

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

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

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

 addDishToCourse(courseName, dishName,dishPrice){

const dish = {
    name: dishName,
    price: dishPrice
}

if(dishName === 'appetizers'){
this._courses.appetizers.push(dish);
}
else if(dishName === 'mains'){
this._courses.mains.push(dish);
}
else if(dishName === 'desserts'){
this._courses.desserts.push(dish);
}
},

getRandomDishFromCourse(courseName){
  let dishes = this._courses[courseName];
  let i = Math.floor(Math.random()*dishes.length);
  return dishes[i];
},

generateRandomMeal(){
  const appetizer = this.getRandomDishFromCourse('appetizers');

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

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

let total = appetizer[price] + main[price] + dessert[price]; 

return `'You've ordered ${appetizer.name}, ${main.name} and ${dessert.name}.The total is ${total} francs CFA. We thank you for your purchase.`
 
}

//end
}

//(courseName, dishName,dishPrice

menu.addDishToCourse( 'mains', 'Thiebou Yapp', 1500);
menu.addDishToCourse('mains', 'Thiebou Dieune', 2000 );
menu.addDishToCourse('appetizers','salade', 500);
menu.addDishToCourse('mains', 'Frites et poulet', 3500);
menu.addDishToCourse('desserts', 'cake', 500);
menu.addDishToCourse('desserts', 'glace italienne', 900);
menu.addDishToCourse('appetizers','tomates et concombre', 1200);
menu.addDishToCourse('desserts','jus Bouye/Bissap', 6100);
menu.addDishToCourse('desserts', 'soft bread', 4500);

//let meal = menu.generateRandomMeal(); 
//console.log(meal);
console.log(menu);
/*let x = menu.courses;
console.log(x)*/

If you console.log(dishName); before the first if statement, what do you see? Is dishName ever going to be ‘appetizers’, ‘mains’ or ‘desserts’?

Beyond that, do you need any if statements? If we already know which array to push the new dish object to, we can use the following syntax: this._courses[courseName].push(dish);

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

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

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

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

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

 addDishToCourse(courseName, dishName,dishPrice){

const dish = {
    name: dishName,
    price: dishPrice
};

console.log(dishName);

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


},

getRandomDishFromCourse(courseName){
  let dishes = this._courses[courseName];
  let i = Math.floor(Math.random()*dishes.length);
  return dishes[i];
},

generateRandomMeal(){
  const appetizer = this.getRandomDishFromCourse('appetizers');

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

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

let total = appetizer[price] + main[price] + dessert[price]; 

return `'You've ordered ${appetizer.name}, ${main.name} and ${dessert.name}.The total is ${total} francs CFA. We thank you for your purchase.`
 
}

//end
}

//(courseName, dishName,dishPrice

menu.addDishToCourse( 'mains', 'Thiebou Yapp', 1500);
menu.addDishToCourse('mains', 'Thiebou Dieune', 2000 );
menu.addDishToCourse('appetizers','salade', 500);
menu.addDishToCourse('mains', 'Frites et poulet', 3500);
menu.addDishToCourse('desserts', 'cake', 500);
menu.addDishToCourse('desserts', 'glace italienne', 900);
menu.addDishToCourse('appetizers','tomates et concombre', 1200);
menu.addDishToCourse('desserts','jus Bouye/Bissap', 6100);
menu.addDishToCourse('desserts', 'soft bread', 4500);

//let meal = menu.generateRandomMeal(); 
//console.log(meal);
console.log(menu);
/*let x = menu.courses;
console.log(x)*/

The result:

Thiebou Yapp
Thiebou Dieune
salade
Frites et poulet
cake
glace italienne
tomates et concombre
jus Bouye/Bissap
soft bread
{ _courses: 
   { appetizers: [ [Object], [Object] ],
     mains: [ [Object], [Object], [Object] ],
     desserts: [ [Object], [Object], [Object], [Object] ] },
  appetizers: [Getter/Setter],
  mains: [Getter/Setter],
  desserts: [Getter/Setter],
  courses: [Getter],
  addDishToCourse: [Function: addDishToCourse],
  getRandomDishFromCourse: [Function: getRandomDishFromCourse],
  generateRandomMeal: [Function: generateRandomMeal] }

Now the function is working right.

1 Like

I remove the comments for the rest. Everything is working. But I have one last question since price is containing a number when we want to have the value we should do main[price] instead of main.price?

MY code only give the expected result once I replace all the courseName[price] to courseName.price.

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

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

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

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

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

 addDishToCourse(courseName, dishName,dishPrice){

const dish = {
    name: dishName,
    price: dishPrice
};

console.log(dishName);
console.log(dishPrice);


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


},

getRandomDishFromCourse(courseName){
  let dishes = this._courses[courseName];
  let i = Math.floor(Math.random()*dishes.length);
  return dishes[i];
},

generateRandomMeal(){
  const appetizer = this.getRandomDishFromCourse('appetizers');

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

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

let total = appetizer.price + main.price + dessert.price; 

return `'You've ordered ${appetizer.name}, ${main.name} and ${dessert.name}.The total is ${total} francs CFA. We thank you for your purchase.`
 
}

//end
}

//(courseName, dishName,dishPrice

menu.addDishToCourse( 'mains', 'Thiebou Yapp', 1500);
menu.addDishToCourse('mains', 'Thiebou Dieune', 2000 );
menu.addDishToCourse('appetizers','salade', 500);
menu.addDishToCourse('mains', 'Frites et poulet', 3500);
menu.addDishToCourse('desserts', 'cake', 500);
menu.addDishToCourse('desserts', 'glace italienne', 900);
menu.addDishToCourse('appetizers','tomates et concombre', 1200);
menu.addDishToCourse('desserts','jus Bouye/Bissap', 6100);
menu.addDishToCourse('desserts', 'soft bread', 4500);

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


And in the console I have:

You've ordered salade, Thiebou Dieune and soft bread.The total is 7000 francs CFA. We thank you for your purchase.

You have 2 options for accessing an object’s keys.

Dot notation:

console.log(myObject.myKey);

Bracket notation:

console.log['myKey']; //must be in quotes when using the actual key name instead of a variable

You can use dot notation as long as the name of the key contains no special characters or spaces. You can always use bracket notation.

1 Like

thank you so much for your help @midlindner . I’ll pay more attention next time.

1 Like