Java script objects

the error says regarding the undefine price , but even in the video to help is done like that I do not understand where is it wrong. please help

working from screenshot is very tricky. Please copy paste your code to the forum

we start by in generateRandomMeal and add a .log() to figure out which menu item is undefined:

console.log(appetizer, mains, desserts)
const totalPrice = ..

then we could add .logs() in addDishToCourse to see why the dish doesn’t get properly added. Without understand why and where the problems from, we can’t debug it.

.log() is one way to get more information to trouble shoot the problem. See if you can use this, and see how far you get :slight_smile:

let menu = {
  _courses: {
   appetizers:[] ,
   mains: [],
   desserts:[] ,
    },
 get appetizers () {
  return this._courses.appetizers;
 },
get mains () {
  return this._courses.mains;
 },
 get desserts() {
  return this._courses.desserts;
 },
 set appetizers(appetizers){
   this._courses.appetizers = appetizers;
 },
 set mains(mains){
   this._courses.mains = mains;
   },
    set desserts(desserts){
   this._courses.desserts = desserts;
   },
get courses(){
  return {
    appetizers: this.appetizers,
    mains: this.mains,
    desserts: this.desserts,
  };
},
addDishToCourse( courseName,dishName, dishPrice ){
  const dish = {
    name: dishName,
    price: dishPrice,
  };
  return this._courses[courseName].push(dish);
},
getRandomDishFromCourse(courseName){
const dishes = this._courses[courseName];
const randomIndex = Math.floor(Math.random()* dishes.lenght);
return dishes[randomIndex];
},
generateRandomMeal(){
  const appetizer = this.getRandomDishFromCourse ('appetizers');
  const main = this.getRandomDishFromCourse ('mains');
 const dessert = this.getRandomDishFromCourse('desserts');
  const totalPrice = appetizer.price + mains.price + dessert.price ;
  return ` Your Meal is ${appetizer.name} ,  ${main.name}, and ${dessert.name} and the total price is ${totalPrice}`;
}
};

menu.addDishToCourse('appetizers', 'salad', 8.50);
menu.addDishToCourse('appetizers', 'mushroomsoup', 6.50);
menu.addDishToCourse('appetizers', 'tofu', 5.50);

menu.addDishToCourse('mains', 'beefsteak', 8.50);
menu.addDishToCourse('mains', 'ribjuicysheep', 6.50);
menu.addDishToCourse('mains', 'chickenkebab', 5.50);

menu.addDishToCourse('desserts', 'icecream', 8.50);
menu.addDishToCourse('desserts', 'chocolatemouse', 6.50);
menu.addDishToCourse('desserts', 'sweetcornwithpotato', 5.50);

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

Did you try to add .log() to see if you could narrow down the problem?

actually, looking at your _courses:

generateRandomMeal(){
  console.log(this._courses);

looks like the _courses object gets filled properly. Which indicates a problem with retrieving the dishes (getRandomDishFromCourse)

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.