Skip to content

Instantly share code, notes, and snippets.

@simonblakemore
Created October 16, 2017 11:37
Show Gist options
  • Save simonblakemore/1e916a5e73dea1bb172f33a973fc4f43 to your computer and use it in GitHub Desktop.
Save simonblakemore/1e916a5e73dea1bb172f33a973fc4f43 to your computer and use it in GitHub Desktop.
Codecademy: Meal Maker Lesson - Day 15
const menu = {
_courses: {
_appetizers: [],
_mains: [],
_desserts: [],
get appetizers() {
return this._appetizers;
},
set appetizers(appetizersIn) {
this._appetizers = appetizersIn;
},
get mains() {
return this._mains;
},
set mains(mainsIn) {
this._mains = mainsIn;
},
get desserts() {
return this._desserts;
},
set desserts(dessertsIn) {
this._desserts = dessertsIn;
},
},
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} and ${dessert.name}. The price is $${totalPrice.toFixed(2)}.`;
},
};
menu.addDishToCourse('appetizers', 'Ceasar Salad', 4.25);
menu.addDishToCourse('appetizers', 'Prawn Coctail', 4.25);
menu.addDishToCourse('appetizers', 'Garlic Bread', 3.50);
menu.addDishToCourse('mains', 'Lasagna', 9.75);
menu.addDishToCourse('mains', 'Ribeye Steak', 14.95);
menu.addDishToCourse('mains', 'Fish & Chips', 12.95);
menu.addDishToCourse('desserts', 'Cheese Cake', 4.50);
menu.addDishToCourse('desserts', 'Creme Brule', 4.25);
menu.addDishToCourse('desserts', 'Cheese Board', 3.25);
let meal = menu.generateRandomMeal();
console.log(meal);
@phantware
Copy link

can somehow explain the project because I don't really understand it, I was just following up

@Chknbone
Copy link

Oh man. I am so glad to see others struggled with this one too. It made no sense to me and the steps were HORRIBLE written. Thanks for posting this.

@ArtemBYN
Copy link

ArtemBYN commented Jun 7, 2020

Thank you
at some point, the error "Cannot read property 'push' of undefined" occurred and it is not clear why

@rrabi10
Copy link

rrabi10 commented Sep 3, 2020

Thanks!
I was struggling for this one for a long time

@Serg911
Copy link

Serg911 commented Oct 3, 2020

Thanks, simonblakemore.
I too was getting "cannot read property 'push' of undefined" error. And couldn't get past it after spending too much time on this. As a last attempt, reset the project to start again and closely follow the instructions, only to run into the same error message again. Comparing my code to yours, I could see a couple of differences, but not to a degree where it woul be a factor (a semi colon here and there, a coma, and this line: this._courses[courseName].push(dish); I had as this._courses.[courseName].push(dish) -period before the opening bracket.
This project was more challenging than I expected

@Gregprod33
Copy link

Even after reading the code solution, it's hard to understand the meaning of all steps, i wish they could they had explain it a bit more...
And why not to use classes object here ?

@noni007
Copy link

noni007 commented Nov 26, 2020

This was so crazy. Imagine taking your time, referring to previous object lessons before the project to make sure you did not miss something. At some point I had to ask myself if I had tried all I could. Went on the forum as well and still could not understand why the"push method on dishes" was throwing a flag. Just glad to know I was not running crazy when I saw the comments here @simonblackmore thanks for this.

Copy link

ghost commented Mar 6, 2021

Thank you!!!

@smyja
Copy link

smyja commented Dec 26, 2021

This particular project on code academy was terrible. Lol.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment