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

194
Views
indefinido al intentar calcular el precio en el objeto

Estoy tomando el curso Codecademy Javascript. Tengo la tarea de hacer un menú. Acabo de aprender sobre objetos y todavía estoy un poco confuso sobre la distinción entre métodos y propiedades en este momento. He estado siguiendo su video tratando de depurar mi código, pero por lo que puedo decir, debería funcionar igual, si no ser exactamente igual.

 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);

La consola devuelve "ReferenceError: los aperitivos no están definidos" en la línea 48 cuando calculo el precio total.

Como pregunta secundaria, ¿importan las posiciones de las funciones getter y setter? Gracias por adelantado.

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

0

Tu nombre de variable es appetizer

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

pero estás usando 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!