Meal Maker

Hi all,

I’m struggling with the Meal Maker project, and can’t find what’s wrong with my solution.

My setter methods don’t seem to be working, specifically, the names of the dishes aren’t stored properly (it prints out [object] instead of the actual string). The item price, on the other hand, seems to be set and getted (gotten? :smile:) correctly, the total is calculated.

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: []
  },
  
  get appetizers() {
  },
  set appetizers(appetizersIn) {
  },
  get mains() {   
  },
  set mains(mainsIn) { 
  },
  get desserts() {
  },
  set desserts(dessertsIn) {
  },
  
  get 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 randomNumber = Math.floor(Math.random() * dishes.length);
    return dishes[randomNumber];
  },
  
  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 suggested menu for today: ${appetizer} to start with, then a tasty ${main}, and finally, a ${dessert}. It will cost you $${totalPrice}.`
  }
}

menu.addDishToCourse('appetizers', 'Foie Gras', 12);
menu.addDishToCourse('appetizers', 'Crackers and Cheese', 9);
menu.addDishToCourse('appetizers', 'Bread with Lard', 3);

menu.addDishToCourse('mains', 'Ham and Eggs', 15);
menu.addDishToCourse('mains', 'Steak', 25);
menu.addDishToCourse('mains', 'Pizza', 17);

menu.addDishToCourse('desserts', 'Cake', 3);
menu.addDishToCourse('desserts', 'Coffee', 3);
menu.addDishToCourse('desserts', 'Biscuits', 2.5);

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

If I run the code:

Your suggested menu for today: [object Object] to start with, then a tasty [object Object], and finally, a [object Object]. It will cost you $28.5.

Thank you in advance for your help!

seems to works fine for the most part?

here:

return `Your suggested menu for today: ${appetizer} to start with, then a tasty ${main}, and finally, a ${dessert}. It will cost you $${totalPrice}.`

appetizer is indeed an object, see addDishToCourse method

yet when you want the price of the object/appetizer:

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

you do it correctly, i am struggling to understand why then getting the .name is such a problem.

3 Likes

You’re right! I completely forgot to call the name key within the appetizer/main/dessert object. This solves the problem:

return `Your suggested menu for today: ${appetizer.name} to start with, then a tasty ${main.name}, and finally, a ${dessert.name}. It will cost you $${totalPrice}.`

It was simply an oversight on my part. Thank you for your kind help!

1 Like

no problem, what also might help is to log the object to see what you might have missed. Teaching yourself to debug can be really valuable.

2 Likes

Hi everyone
this is my code:

let menu = {
  _courses: {
  appetizers: [],
  mains: [],
  desserts: []
},
  get appetizers(){
    
     
  },
  set appetizers(input){
    
  },
  get mains(){
    
     
  },
  set mains(input2){
    
  },
  get desserts(){
    
     
  },
  set desserts(input3){
    
  },
  get _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 randindex =Math.floor(Math.random()*dishes.length); 
    return dishes[randindex];  
    
  }
	generateRandomMeal(){
    const appetizer=getRandomDishFromCourse('appetizers');
    const main=getRandomDishFromCourse('mains');
    const dessert=getRandomDishFromCourse('desserts');
    const totalprice = appetizer.price + main.price +dessert.price;
    return `Your meal is ${appetizer.name}, ${main.name}, ... The price is $${totalPrice}.`;    
  }
};

menu.addDishToCourse('appetizers','kaved of',50);
menu.addDishToCourse('appetizers','salad',40);
menu.addDishToCourse('appetizers','soup',60);
menu.addDishToCourse('mains','chicken',80);
menu.addDishToCourse('mains','hotdog',60);
menu.addDishToCourse('mains','steak',120);
menu.addDishToCourse('desserts','ice cream',50);
menu.addDishToCourse('desserts','cake',60);
menu.addDishToCourse('desserts','cookies',40);
let meal = menu.generateRandomMeal();
console.log(meal);

and i get the following error message:

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:39
  getRandomDishFromCourse(courseName)
  ^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Unexpected identifier
    at createScript (vm.js:53:10)
    at Object.runInThisContext (vm.js:95:10)
    at Module._compile (module.js:543:28)
    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)

what’s wrong, why do i get the error message?
thank’s
dorit

@doritfish check for missing commas

ok thanks a lot you are right i fixed it and put some commas but now i get this error, any idea:

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:36
    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:36:37)
    at Object.<anonymous> (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:55: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)

Hello, @doritfish.
If you remove the underscore from your courses getter, that will clear this error.

This method will then need some attention. In order to invoke the getRandomDishFromCourse() method of the menu object, you’ll need to use the same syntax you used to invoke the addDishToCourse() method.

Hi midlindner

I changed the typo and underscore and it still gives me the same error…
thanks for your quick replays:)
Dorit

Can you post your current code?

sure:

let menu = {
  courses: {
  appetizers: [],
  mains: [],
  desserts: []
},
  get appetizers(){
    
     
  },
  set appetizers(input){
    
  },
  get mains(){
    
     
  },
  set mains(input2){
    
  },
  get desserts(){
    
     
  },
  set desserts(input3){
    
  },
  get courses(){
    return {appetizers:this.appetizers,mains:this.mains,                         desserts:this.desserts};
  },
  addDishToCourse(courseName, dishName, dishPrice){
    const dish = {
      name: dishName,
      price: dishPrice
    };
     this.courses[courseName].push(dish);   
  },
  
  getRandomDishFromCourse(courseName)
  {
    const dishes = this.courses[courseName];
    const randindex =Math.floor(Math.random()*dishes.length); 
    return dishes[randindex];  
    
  },
	generateRandomMeal(){
    const appetizer=getRandomDishFromCourse('appetizers');
    const main=getRandomDishFromCourse('mains');
    const dessert=getRandomDishFromCourse('desserts');
    const totalprice = appetizer.price + main.price +dessert.price;
    return `Your meal is ${appetizer.name}, ${main.name}, ... The price is $${totalPrice}.`;
  }
  
};

menu.addDishToCourse('appetizers','kaved of',50);
menu.addDishToCourse('appetizers','salad',40);
menu.addDishToCourse('appetizers','soup',60);
menu.addDishToCourse('mains','chicken',80);
menu.addDishToCourse('mains','hotdog',60);
menu.addDishToCourse('mains','steak',120);
menu.addDishToCourse('desserts','ice cream',50);
menu.addDishToCourse('desserts','cake',60);
menu.addDishToCourse('desserts','cookies',40);
let meal = menu.generateRandomMeal();
console.log(meal);

Possibly the error as well?

np:

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:36
     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:36:30)
    at Object.<anonymous> (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:56: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)

You need the underscore in the addDishToCourse() method: this._courses[courseName].push(dish), but not in the getter declaration.

still not working for me…

Next, you’ll likely have this error:
TypeError: Cannot read property 'appetizers' of undefined

In your generateRandomMeal() method, you call or invoke the getRandomDishFromCourse() method three times to assign appetizer, main & dessert. getRandomDishFromCourse() cannot be accessed directly. It belongs to the menu object.

although the error message changed a little bit:

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

TypeError: Cannot read property 'appetizers' of undefined
    at Object.addDishToCourse (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:36:19)
    at Object.<anonymous> (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:56: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)

ok so i need to add this before the getRandomDishFromCourse? like this:

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

Not quite. Refer to your calling the addDishToCourse() method:

menu.addDishToCourse('appetizers','kaved of',50);

Same thing here. Since getRandomDishFromCourse() also belongs to the menu object:

const appetizer=menu.getRandomDishFromCourse('appetizers');

EDIT: My apologies. I was thinking we were calling the method from outside the object. You were correct in using this.
menu will work as well, but this is more appropriate in this context.