Https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/learn-javascript-objects/projects/meal-maker

Hi everyone!
I am stuck on https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/learn-javascript-objects/projects/meal-maker
And even after watching the help video many times, I still get the error message: “TypeError: Cannot read property ‘price’”
Can someone help me understand?

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
  },
  get appetizers() {
    return this._courses.appetizers;
  },
	set appetizers(appetizers) {
    this._courses.appetizers = appetizers;
	},
  get mains() {
    return this._courses.mains;
  },
	set mains(mains) {
  	this._courses.mains = mains;
	},
  get desserts() {
		return this._courses.desserts;
  },
	set desserts(desserts) {
  	this._courses.desserts = desserts;
},
  
  get course() { 
  return {
    appetizers: this.appetizers, // this uses the appetizer getter method
    mains: this.mains,
    desserts: this.desserts,
  };
  },
  
  addDishToCourse(courseName, dishName, dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice, 
    };
    return this._courses[courseName].push(dish);	// push this dish object into the appropriate array in your menu‘s _courses object based on what courseName was passed in. (use your setter method)
  },
  
  getRandomDishFromCourse(courseName) {
    const dishes = this._courses[courseName];
    const randomIndex = Math.floor(Math.random() * dishes.lenght);
    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}. And the total price is ${totalPrice}`
    },
}


menu.addDishToCourse('appetizers', 'roccula salad', 4.50);
menu.addDishToCourse('appetizers', 'tapas', 5.50);
menu.addDishToCourse('appetizers', 'tapenade', 4.25);
menu.addDishToCourse('appetizers', 'guacamole', 5.00);
menu.addDishToCourse('appetizers', 'gaspacho', 3.50);

menu.addDishToCourse('mains', 'raclette', 14.50);
menu.addDishToCourse('mains', 'chicken massala', 12.50);
menu.addDishToCourse('mains', 'gigantic pizza', 15.00);
menu.addDishToCourse('mains', 'fish and chips', 16.00);
menu.addDishToCourse('mains', 'pasta', 3.50);

menu.addDishToCourse('desserts', 'tiramisu', 4.50);
menu.addDishToCourse('desserts', 'chocolate cake', 5.50);
menu.addDishToCourse('desserts', 'madeleines', 4.25);
menu.addDishToCourse('desserts', 'berry salad', 5.00);
menu.addDishToCourse('desserts', 'pancakes', 3.50);

meal1 = menu.generateRandomMeal();
console.log(meal1);

This is where I get the error message

@pierre0512

You misspelled something :stuck_out_tongue:

I think it will work if you fix that
Steven

2 Likes

Thanks a lot @stevencopeland!
Do you know how come I didn’t get any error on that line, but on a completely different one? It made my look into the wrong place all that time :exploding_head:

1 Like

The reason for that was because you were calling the function on that line.

This function right here:

Was called right here:

const appetizer = this.getRandomDishFromCourse('appetizers');
const main = this.getRandomDishFromCourse('mains');
const dessert = this.getRandomDishFromCourse('desserts');
const totalPrice = appetizer.price + main.price + dessert.price;

Which is why you got the answer there. lol yeah it messed me up too when I was searching your code for the solution.
Quick question. If this solved your problem, would you check the solved button? Not a big deal, but that will allow the topic to close.

2 Likes