Meal Maker - Reference Error Desserts Undefined

Hey codecademy community!

I’m stuck on the Meal Maker project. My code is pulling up “ReferenceError: dessert is not defined”.

Can anyone help me see what I did wrong?

Thank you

const menu = {
  _courses: {
    _appetizers: [],
    _mains: [],
    _desserts: [],
    
    get appetizers() {
      return this._appetizers;
    },
    get mains() {
      return this._mains;
    },
    get desserts() {
      return this._desserts;
    },
    set appetizers(appetizersIn) {
      this._appetizers = appetizersIn;
    },
    set mains(mainsIn) {
      this._mains = mainsIn;
    },
    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 desserts = 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', 'Calamari', 11.25);
menu.addDishToCourse('appetizers', 'Roquefort Pear Salad', 12.50);
menu.addDishToCourse('appetizers', 'Soup De Jour', 10.95);

menu.addDishToCourse('mains', 'Mediterranean Sea Bass', 26.35);
menu.addDishToCourse('mains', 'Butternut Squash Ravioli', 22.50);
menu.addDishToCourse('mains', 'Roast Beef Tenderloin with Wine Sauce', 31.95);

menu.addDishToCourse('desserts', 'Profiteroles', 11.25);
menu.addDishToCourse('desserts', 'Lemon Blueberry Pavlova', 12.50);
menu.addDishToCourse('desserts', 'Tiramisu Crepe Cake', 10.95);

let meal = menu.generateRandomMeal();

console.log(meal);

I notice that you define the variable as “desserts” here, but then when you reference it, you call it “dessert”

const desserts = 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)}.`;

Either that or I read your code wrong.

Hi Steven,

Thank you for taking the time to respond. I omitted the plurality as I’ve seen it throughout the course as a frequent practice.

However, to eliminate all possible sources of error, I added back the plurals but am still faced with a reference error - now regarding appetizers:

“ReferenceError: appetizers is not defined”

hmm

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 desserts = this.getRandomDishFromCourse('desserts');
    const totalPrice = appetizers.price + mains.price + desserts.price;
  
    return `Your meal is ${appetizers.name}, ${mains.name} and ${desserts.name}. The price is $${totalPrice.toFixed(2)}.`;
  },
};
  
menu.addDishToCourse('appetizers', 'Calamari', 11.25);
menu.addDishToCourse('appetizers', 'Roquefort Pear Salad', 12.50);
menu.addDishToCourse('appetizers', 'Soup De Jour', 10.95);

menu.addDishToCourse('mains', 'Mediterranean Sea Bass', 26.35);
menu.addDishToCourse('mains', 'Butternut Squash Ravioli', 22.50);
menu.addDishToCourse('mains', 'Roast Beef Tenderloin with Wine Sauce', 31.95);

menu.addDishToCourse('desserts', 'Profiteroles', 11.25);
menu.addDishToCourse('desserts', 'Lemon Blueberry Pavlova', 12.50);
menu.addDishToCourse('desserts', 'Tiramisu Crepe Cake', 10.95);

let meal = menu.generateRandomMeal();

console.log(meal);

Same problem?

return `Your meal is ${appetizers.name}, ${mains.name} and ${desserts.name}. The price is $${totalPrice.toFixed(2)}.`;

But you define the variable as appetizer.

1 Like

Oh wow, I just realised you meant it’s inconsistent.

Just fixed it and now it works. Thank you so much!! :partying_face:

Here’s the code:

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', 'Calamari', 11.25);
menu.addDishToCourse('appetizers', 'Roquefort Pear Salad', 12.50);
menu.addDishToCourse('appetizers', 'Soup De Jour', 10.95);

menu.addDishToCourse('mains', 'Mediterranean Sea Bass', 26.35);
menu.addDishToCourse('mains', 'Butternut Squash Ravioli', 22.50);
menu.addDishToCourse('mains', 'Roast Beef Tenderloin with Wine Sauce', 31.95);

menu.addDishToCourse('desserts', 'Profiteroles', 11.25);
menu.addDishToCourse('desserts', 'Lemon Blueberry Pavlova', 12.50);
menu.addDishToCourse('desserts', 'Tiramisu Crepe Cake', 10.95);

let meal = menu.generateRandomMeal();

console.log(meal);
1 Like

Oh sorry! Meant to be at little clearer :smiley:

1 Like