Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
undefined while trying to calculate price in object

I'm taking the Codecademy Javascript course. I'm tasked with making a menu. I just learned about objects and I'm still a little fuzzy on the distinction between methods and properties right now. I've been following along to their video trying to debug my code but as far as I can tell it should function the same, if not be exactly the same.

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 courses() {
    return {
      appetizers: this.appetizers,
      mains: this.mains,
      desserts: this.desserts,
    };
  },
  getRandomDishFromCourse(courseName) {
    const dishes = this._courses[courseName];
    const randomIndex = Math.floor(Math.random() * dishes.length);
  },
  addDishToCourse(courseName, dishName, dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice,
    };
    this._courses[courseName].push(dish);
  },
  generateRandomMeal() {
    const appetizer = this.getRandomDishFromCourse('appetizers');
    const mains = this.getRandomDishFromCourse('mains');
    const desserts = this.getRandomDishFromCourse('desserts');
    const totalPrice = appetizers.price + mains.price + desserts.price;
    return `Your meal is ${appetizers.name}, ${mains.name}, ${desserts.name}. The total price is ${totalPrice}.`;
  }
};

menu.addDishToCourse('appetizers', 'Caesar Salad', 9);
menu.addDishToCourse('appetizers', 'Octopus', 18);
menu.addDishToCourse('appetizers', 'Scallop and Lobster Stuffed Crepe', 22);

menu.addDishToCourse('mains', 'Berkshire Porkchop', 45);
menu.addDishToCourse('mains', 'Filet Mignon', 43);
menu.addDishToCourse('mains', 'Spicy Lobster Pasta', 47);

menu.addDishToCourse('desserts', 'Deconstructed Mud Pie', 18);
menu.addDishToCourse('desserts', 'Creme Brulee', 14);
menu.addDishToCourse('desserts', 'Cheesecake', 18);

const meal = menu.generateRandomMeal();
console.log(meal);

The console is returning "ReferenceError: appetizers is not defined" at the 48th line when I'm calculating totalPrice.

As a secondary question, do the positions of the getter and setter functions matter? Thanks in advance.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your variable name is appetizer

const appetizer = this.getRandomDishFromCourse('appetizers');

but you using appetizers

appetizers.price
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!