Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

191
Vistas
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 Respuestas
Responde la pregunta

0

Your variable name is appetizer

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

but you using appetizers

appetizers.price
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda