Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

196
Visualizações
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 Respostas
Responde à pergunta

0

Your variable name is appetizer

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

but you using appetizers

appetizers.price
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda