MEAL MAKER .push not a property for undefined


#1

MEAL MAKER is making me crazy! Hello everybody, I’m in a frantic search for wisdom. Can anyone enlighten me?!

This is my code:

const menu = {
  
  generateRandomMeal() {
    const appetizer = this.getRandomDishFromCourse("appetizers");
    
    const main = this.getRandomDishFromCourse("mains");
    
    const dessert = this.getRandomDishFromCourse("desserts");
    
    const totalPrice = appetizer.dishPrice + main.dishPrice + dessert.dishPrice;
    
    return "Your meal consists of " + appetizer + ", " + main + " and " + dessert + ". The total price is " + totalPrice + "."
  },
  
  getRandomDishFromCourse(courseName) {
    
    let dishes = menu._courses[courseName];
    
    let randomIndex = Math.floor(Math.random(dishes.length));
    
    return dishes[randomIndex]
    
  },
  
    
  get courses(){
    
    return {
      appetizers: this._courses.appetizers ,
      mains: this._courses.mains ,
      desserts: this._courses.desserts ,
    }
    
  },
  
  set courses(coursesIn){
    
    this._courses.push(coursesIn);
    
  },
  
  
  _courses: {
    _appetizers: [],
    
    get appetizers(){
      
      return this._appetizers;
      
    },
    
    set appetizers(appetizerIn){
      
      this._appetizers.push(appetizerIn);
    },
    
    _mains: [],
    
    get mains(){
      
      return this._mains;
      
    },
    
    set mains(mainIn){
      
     this._mains.push(mainIn);
      
    },
    
    
    _desserts: [],
    
    get desserts(){
      
      return this._desserts;
      
    },
    
    set desserts(dessertIn){
      
      this._desserts.push(dessertIn);
      
    },
    
  },
  
  addDishToCourse (courseName, dishName, dishPrice){
    
    const dish = {
      name: dishName,
      price: dishPrice,
    };
    
   this._courses[courseName].push(dish);
  },
}

menu.addDishToCourse("appetizers", "Coctel de camaron", 140);
menu.addDishToCourse("appetizers", "Boquerones", 80);
menu.addDishToCourse("appetizers", "rabas", 100);

menu.addDishToCourse("mains", "Pasta de mar", 140);
menu.addDishToCourse("mains", "Pesca del día", 180);
menu.addDishToCourse("mains", "Sorpresa de mariscos", 200);

menu.addDishToCourse("dessert", "Creme Brulé", 110);
menu.addDishToCourse("dessert", "Sopa dulce", 110);
menu.addDishToCourse("dessert", "Manzana asada", 80);

let meal = menu.generateRandomMeal();

console.log(meal);

I keep getting this error. Just don’t know what else to do:

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

#2

Look for where the variable is supposed to be defined. Or for issues like this one…

Math.random() * dishes.length

#3

Thank mtf!

The Math.random was a silly mistake for doing it all again really fast. I understand that there is some sort of problem with the definition of _courses inside of menu, but i think I’ve defined it inside the menu object.

Can you hint me more?


#4

There may not be a problem with where it is defined. This error involved generating a faulty index, and thus returning an undefined value.


#5

I can’t seem to get the error. Can you tell me a little bit more?


#6
dessert  =>  desserts

Change the course name to ‘desserts’.

Also, replace two lines in getRandomMeal to,

    const totalPrice = appetizer.price + main.price + dessert.price;
    
    return "Your meal consists of " + appetizer.name + ", " + main.name + " and " + dessert.name + ". The total price is " + totalPrice + "."
console test run
 > const menu = {
  
  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 consists of " + appetizer.name + ", " + main.name + " and " + dessert.name + ". The total price is " + totalPrice + "."
  },
  
  getRandomDishFromCourse(courseName) {
    
    let dishes = menu._courses[courseName];
    
    let randomIndex = Math.floor(Math.random() * dishes.length);
    
    return dishes[randomIndex]
    
  },
  
    
  get courses(){
    
    return {
      appetizers: this._courses.appetizers ,
      mains: this._courses.mains ,
      desserts: this._courses.desserts ,
    }
    
  },
  
  set courses(coursesIn){
    
    this._courses.push(coursesIn);
    
  },
  
  
  _courses: {
    _appetizers: [],
    
    get appetizers(){
      
      return this._appetizers;
      
    },
    
    set appetizers(appetizerIn){
      
      this._appetizers.push(appetizerIn);
    },
    
    _mains: [],
    
    get mains(){
      
      return this._mains;
      
    },
    
    set mains(mainIn){
      
     this._mains.push(mainIn);
      
    },
    
    
    _desserts: [],
    
    get desserts(){
      
      return this._desserts;
      
    },
    
    set desserts(dessertIn){
      
      this._desserts.push(dessertIn);
      
    },
    
  },
  
  addDishToCourse (courseName, dishName, dishPrice){
    
    const dish = {
      name: dishName,
      price: dishPrice,
    };
    
   this._courses[courseName].push(dish);
  },
}
<- undefined
 > {
menu.addDishToCourse("appetizers", "Coctel de camaron", 140);
menu.addDishToCourse("appetizers", "Boquerones", 80);
menu.addDishToCourse("appetizers", "rabas", 100);

menu.addDishToCourse("mains", "Pasta de mar", 140);
menu.addDishToCourse("mains", "Pesca del día", 180);
menu.addDishToCourse("mains", "Sorpresa de mariscos", 200);

menu.addDishToCourse("desserts", "Creme Brulé", 110);
menu.addDishToCourse("desserts", "Sopa dulce", 110);
menu.addDishToCourse("desserts", "Manzana asada", 80);
}
<- undefined
 > menu.generateRandomMeal()
"Your meal consists of rabas, Pasta de mar and Creme Brulé. The total price is 350."

#7

Wooops, not seen those typos and forgot about each object having properties, d’ah. Now I’m getting this new error:

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

#8

Compare your code the repaired version I tested in the console. Re-post what you have so far and we can have another look.


#9

I’ve cleaned up and re-organized the code. _courses does not need a setter. It has an addDishToCourse method for that purpose.

const menu = {
  _courses: {
    _appetizers: [],
    _mains: [],
    _desserts: [],
    get appetizers(){
      return this._appetizers;
    },
    set appetizers(appetizerIn){
      this._appetizers.push(appetizerIn);
    },
    get mains(){
      return this._mains;
    },
    set mains(mainIn){
     this._mains.push(mainIn);
    },
    get desserts(){
      return this._desserts;
    },
    set desserts(dessertIn){
      this._desserts.push(dessertIn);
    },
  },
  get courses(){
    return {
      appetizers: this._courses.appetizers,
      mains: this._courses.mains,
      desserts: this._courses.desserts,
    }
  },
//  set courses(coursesIn){
//    this._courses.push(coursesIn);
//  },
  addDishToCourse (courseName, dishName, dishPrice){    
    const dish = {
      name: dishName,
      price: dishPrice,
    };    
    this._courses[courseName].push(dish);
  },
  getRandomDishFromCourse(courseName) {
    let dishes = menu._courses[courseName];
    let 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 consists of " + appetizer.name + ", " + main.name + " and " + dessert.name + ". The total price is " + totalPrice + "."
  },
};

menu.addDishToCourse("appetizers", "Coctel de camaron", 140);
menu.addDishToCourse("appetizers", "Boquerones", 80);
menu.addDishToCourse("appetizers", "rabas", 100);

menu.addDishToCourse("mains", "Pasta de mar", 140);
menu.addDishToCourse("mains", "Pesca del día", 180);
menu.addDishToCourse("mains", "Sorpresa de mariscos", 200);

menu.addDishToCourse("desserts", "Creme Brulé", 110);
menu.addDishToCourse("desserts", "Sopa dulce", 110);
menu.addDishToCourse("desserts", "Manzana asada", 80);

let meal = menu.generateRandomMeal();

console.log(meal);

#10

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