-
-
Save simonblakemore/1e916a5e73dea1bb172f33a973fc4f43 to your computer and use it in GitHub Desktop.
| 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); |
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
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 ?
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.
Thank you!!!
This particular project on code academy was terrible. Lol.
Thanks!
I was struggling for this one for a long time