Meal Maker


#1

Having some issues with the Meal Maker Project

const menu = {
  _courses:{
     _appetizers:[],
     _mains:[],
     _desserts:[],
  
    get appetizers(){
      return this._appetizers;
    },
    set appetizers(appetizerIn){
      return  this._appetizers.push(appetizerIn);
    },
    get mains(){
      return this._mains;
    },
    set mains(mainsIn){
      return this._mains.push(mainsIn);
    },
    get desserts(){
      return this._dessertsIn;
    },
    set desserts(dessertIn){
      return this._desserts.push(dessertIn);
    }
  },
  get courses(){
     return {
       appetizers: this._courses.appetizers,
       mains: this._courses.mains,
       desserts: this._courses.deserts
     }
   },
 
  addDishToCourse: function(courseName, dishName, dishPrice){
    const dish = {
      name: dishName,
      price: dishPrice
    };
    this._courses[courseName].push(dish);
  },
   
  getRandomDishFromCourse: function(courseName){
    const dishes = (this._courses[coursename]);
    const randomIndex = (math.floor(math.random * dishes.length));
    return dishes[randomIndex];
  },  
 
  generateRandomMeal: function(){
    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}, ${dessert.name}. Your total is $${totalPrice}.`
  },
}
 
 
menu.addDishToCourse("appetizers", "wings", 6.50);
menu.addDishToCourse("appetizers", "chips", 3.00);
menu.addDishToCourse("appetizers", "pretzel", 5.00);
menu.addDishToCourse("mains", "pizza", 12.00);
menu.addDishToCourse("mains", "burger", 9.00);
menu.addDishToCourse("mains", "pasta", 12.00);
menu.addDishToCourse("dessert", "pie", 3.00);
menu.addDishToCourse("dessert", "cake", 3.00);
menu.addDishToCourse("dessert", "cookie", 2.50);
 
let meal = menu.generateRandomMeal();
console.log(meal);

Here’s the error I recieve

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:39
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:39:26)
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:64: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)

Can’t seem to figure this one out…

Thanks for any help.


#2

Edit: I’ve reformatted your opening post for readability.

These setters do not need to return anything.

Math  vs.  math

That’s a start. Still reviewing.


#3

So the setters should just read as,

set appetizers(appetizerIn){ },


#4

Right. Getters return, setters set.

Spotted an error,

That should be just this._desserts.


Since we have ES6 syntax at our disposal, try using the new methods.

Eg.

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

Still trying to find how this._courses[courseName] is not an array (in your code, the cause of the error).


#5

This is a deal breaker.

deserts  =>  desserts

#6

Gotcha!

ReferenceError: coursename is not defined


I changed so many things it’s tough to say where the real issue was but it seems to boil down to the appetizers setter. Needless to say,

const menu = {
  _courses: {
     _appetizers: [],
     _mains: [],
     _desserts: [],  
    set appetizers (appetizer) {
      this._appetizers.push(appetizer);
    },
    set mains (main) {
      this._mains.push(main);
    },
    set desserts (dessert) {
      this._desserts.push(dessert);
    },
    get appetizers () {
      return this._appetizers;
    },
    get mains () {
      return this._mains;
    },
    get desserts () {
      return this._desserts;
    }
  },
  get courses () {
     return {
       appetizers: this._courses.appetizers,
       mains: this._courses.mains,
       desserts: this._courses.desserts
     }
   }, 
  addDishToCourse (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 () {
    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}, ${dessert.name}. Your total is $${totalPrice}.`
  }  
}
 

menu.addDishToCourse("appetizers", "wings", 6.50);
menu.addDishToCourse("appetizers", "chips", 3.00);
menu.addDishToCourse("appetizers", "pretzel", 5.00);

menu.addDishToCourse("mains", "pizza", 12.00);
menu.addDishToCourse("mains", "burger", 9.00);
menu.addDishToCourse("mains", "pasta", 12.00);

menu.addDishToCourse("desserts", "pie", 3.00);
menu.addDishToCourse("desserts", "cake", 3.00);
menu.addDishToCourse("desserts", "cookie", 2.50);

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

console.log(menu.courses)

console.log(menu.generateRandomMeal());

makes it to the end.


#7

Great thanks. I’ll hold onto the final code and try to build it again from scratch.


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.