Meal Maker


#1

https://www.codecademy.com/courses/learn-javascript-objects/projects/meal-maker?course_redirect=introduction-to-javascript

Please help. Error in getRandomDishFromCourse, I do not see why?

My code:

let menu = {
  _courses: {},
    _appetizers:	[],
    _mains: [],
    _desserts: [],		
  
  
  
  get appetizers() {
  	return this._appetizers;
  },
   get mains() {
   	return this._mains; 
  },
  get desserts() {
    return this._desserts;
    
  },
    set appetizers(appetizersIn) {
   return this._appetizers.push(appetizerIn);  
    },
 
    set mains(mainsIn) {
   return this._mains.push(mainIn);   
    },
  
    set desserts(dessertsIn){
      return this._desserts.push(dessertIn);
    }, 
    get courses() {
      return {
      appetizers: this._courses.appetizers,
      mains: this._courses.mains,
      desserts: this._courses.desserts
      }
    }, 
  addDishToCourse (courseName, dishName, dishPrice) {
  let dish= {
  name: dishName,
  price: dishPrice
};
this._courses[courseName].push(dish);
}
  getRandomDishFromCourse(courseName) {
  const  dishes = this._courses[courseName];
  const mathIndex = Math.floor(Math.random() * dishes.length);
 return dishes[mathIndex];
} 
generateRandomMeal() {
  const appetizer = this.getRandomDishFromCourse("appetizer");
  const main = this.getRandomDishFromCourse("main");
  const desserts = this.getRandomDishFromCourse("dessert");
  const totalPrice =  appetizer.price + main.price + desserts.price;
  return `Your meal is ${appetizer.name}, ${mains.name}, ${desserts.name} The price is $${totalPrice}.`;
}
menu.addDishToCourse('appetizers', 'Caesar Salad', 4.25);
menu.addDishToCourse('appetizers', 'Cap Salad', 4.99);
menu.addDishToCourse('appetizers', 'Best Salad', 12.99);
menu.addDishToCourse('mains', 'Soup', 2.92);
menu.addDishToCourse('mains', 'Soupeee', 6.92);
menu.addDishToCourse('mains', 'Borsh', 2.23);
menu.addDishToCourse('desserts', 'Cake', 1.86);
menu.addDishToCourse('desserts', 'Cake2', 1.97);
menu.addDishToCourse('desserts', 'Cake3', 1.99);
let meal = menu.generateRandomMeal();
console.log(meal);
};

#2

methods needs to be separated by comma, you do this fine until the last getter method:

    get courses() {
        return {
            appetizers: this._courses.appetizers,
            mains: this._courses.mains,
            desserts: this._courses.desserts
        }
    }, // this is fine

but then the next method:

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

you forget the comma to separate methods of object.

i would also highly recommend improving the indent of your code to improve readability.


#3

Thank you.
I fixed it.
I still have a problem with menu…addDishToCourse

let menu = {
_courses: {},
_appetizers: [],
_mains: [],
_desserts: [],

get appetizers() {
return this._appetizers;
},
get mains() {
return this._mains;
},
get desserts() {
return this._desserts;

},

set appetizers(appetizersIn) {

return this._appetizers.push(appetizerIn);
},

set mains(mainsIn) {

return this._mains.push(mainIn);
},

set desserts(dessertsIn){
  return this._desserts.push(dessertIn);
}, 


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

addDishToCourse (courseName, dishName, dishPrice) {
let dish= {
name: dishName,
price: dishPrice
};
this._courses[courseName].push(dish);
},
getRandomDishFromCourse(courseName) {
const dishes = this._courses[courseName];
const mathIndex = Math.floor(Math.random() * dishes.length);
return dishes[mathIndex];
},
generateRandomMeal() {
const appetizer = this.getRandomDishFromCourse(“appetizer”);
const main = this.getRandomDishFromCourse(“main”);
const desserts = this.getRandomDishFromCourse(“dessert”);
const totalPrice = appetizer.price + main.price + desserts.price;
return Your meal is ${appetizer.name}, ${mains.name}, ${desserts.name} The price is $${totalPrice}.;
},

menu.addDishToCourse(‘appetizers’, ‘Caesar Salad’, 4.25);
menu.addDishToCourse(‘appetizers’, ‘Cap Salad’, 4.99);
menu.addDishToCourse(‘appetizers’, ‘Best Salad’, 12.99);
menu.addDishToCourse(‘mains’, ‘Soup’, 2.92);
menu.addDishToCourse(‘mains’, ‘Soupeee’, 6.92);
menu.addDishToCourse(‘mains’, ‘Borsh’, 2.23);
menu.addDishToCourse(‘desserts’, ‘Cake’, 1.86);
menu.addDishToCourse(‘desserts’, ‘Cake2’, 1.97);
menu.addDishToCourse(‘desserts’, ‘Cake3’, 1.99);
let meal = menu.generateRandomMeal();
console.log(meal);
};


#4

Problems how? What is different then you expected?

Please post your code with format:

How do I format code in my posts?

so the indent shows, and make sure your indent is right, this improves readability of your code and makes it easier to spot mistakes.


#5

//menu.addDishToCourse('appetizers', 'Caesar Salad', 4.25); - error

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

//`menu.addDishToCourse('appetizers', 'Caesar Salad', 4.25);`  - error
  ,,,
menu.addDishToCourse('appetizers', 'Caesar Salad', 4.25);
menu.addDishToCourse('appetizers', 'Cap Salad', 4.99);
menu.addDishToCourse('appetizers', 'Best Salad', 12.99);
menu.addDishToCourse('mains', 'Soup', 2.92);
menu.addDishToCourse('mains', 'Soupeee', 6.92);
menu.addDishToCourse('mains', 'Borsh', 2.23);
menu.addDishToCourse('desserts', 'Cake', 1.86);
menu.addDishToCourse('desserts', 'Cake2', 1.97);
menu.addDishToCourse('desserts', 'Cake3', 1.99);
let meal = menu.generateRandomMeal();
console.log(meal);

};

#6

What do you think might cause the error?


#8
let menu = {
  _courses: {},
    _appetizers:	[],
    _mains: [],
    _desserts: [],		
  
  
  
  get appetizers() {
  	return this._appetizers;
  },
   get mains() {
   	return this._mains; 
  },
  get desserts() {
    return this._desserts;
    
  },
  
  
    set appetizers(appetizersIn) {
   return this._appetizers.push(appetizerIn);  
    },
 
    set mains(mainsIn) {
   return this._mains.push(mainIn);   
    },
  
    set desserts(dessertsIn){
      return this._desserts.push(dessertIn);
    }, 
  
  
    get courses() {
      return {
      appetizers: this._courses.appetizers,
      mains: this._courses.mains,
      desserts: this._courses.desserts
      }
    }, 
  addDishToCourse (courseName, dishName, dishPrice) {
  let dish= {
  name: dishName,
  price: dishPrice
}
this._courses[courseName].push(dish);
//console.log(courseName + ' ' + dishName + ' ' + dishPrice);
},
  getRandomDishFromCourse(courseName) {
  const  dishes = this._courses[courseName];
  const mathIndex = Math.floor(Math.random() * dishes.length);
 return dishes[mathIndex];
}, 
generateRandomMeal() {
  const appetizer = this.getRandomDishFromCourse("appetizer");
  const main = this.getRandomDishFromCourse("main");
  const desserts = this.getRandomDishFromCourse("dessert");
  const totalPrice =  appetizer.price + main.price + desserts.price; 
  return `Your meal is ${appetizer.name}, ${mains.name}, ${desserts.name} The price is $${totalPrice}.`;
}
};

menu.addDishToCourse('appetizers', 'Caesar Salad', 4.25);
menu.addDishToCourse('appetizers', 'Cap Salad', 4.99);
menu.addDishToCourse('appetizers', 'Best Salad', 12.99);
menu.addDishToCourse('mains', 'Soup', 2.92);
menu.addDishToCourse('mains', 'Soupeee', 6.92);
menu.addDishToCourse('mains', 'Borsh', 2.23);
menu.addDishToCourse('desserts', 'Cake', 1.86);
menu.addDishToCourse('desserts', 'Cake2', 1.97);
menu.addDishToCourse('desserts', 'Cake3', 1.99);
let meal = menu.generateRandomMeal();
console.log(meal);

#9

okay, what about this code?


#10

not working. I do not see an error


#11

there is certainly error:

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

this._courses[courseName] is undefined, as such it doesn’t have a .push() method. Maybe use console.log() statement to trace where a value isn’t passed correctly?


#12

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