Undefined Output

When you run my code the output to the console is “Your meal is undefined, undefined, and undefined. The total price is $8.”

You may have to scroll to the side a bit to see more of the line that is causing the problem.

var 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.desserts = desserts;
  },
set mains(mains) {
   this.mains = mains;
},
set desserts(desserts) {
     this.desserts = desserts;
  },
  get courses() {
    return {
      appetizers,
      mains,
      desserts,
    };
  },
  addDishToCourse (courseName, dishName, dishPrice) { 
   const dish = {
     name: dishName,
     price: dishPrice,
    
   };
  return this._courses[courseName].push(dish);
  },
     getRandomDishFromCourse: function(courseName) {
    var dishes = this._courses[courseName];
     if (dishes !== undefined) {
       var randomIndex = Math.floor(Math.random() * dishes.length);
      return randomIndex;
     }
  },
  
  generateRandomMeal: function() { //This is the problem.
   var appetizer = this.getRandomDishFromCourse('appetizers');
    var main = this.getRandomDishFromCourse('mains');
    var dessert = this.getRandomDishFromCourse('dessert');
    return 'Your meal is ' + this.appetizers.appetizer + ', ' + this.mains.main + ', and ' + this.desserts.dessert + '. The total price is $' +/* this.dish.price */ 8 + '.'; // I put the eight in place of the other thing for testing. I know it's a problem but I am getting there after this isue is solved.
  }
}; 

menu.addDishToCourse('appetizers', 'Bread Loaf', 4.25);
menu.addDishToCourse('appetizers', 'Caesar Salad', 3.50);
menu.addDishToCourse('appetizers', 'Small Soup', 4.50);
menu.addDishToCourse('mains', 'Steak Dinner', 20.00);
menu.addDishToCourse('mains', 'spaghetti', 16.00);
menu.addDishToCourse('mains', 'Turkey Dinner', 18.99);
menu.addDishToCourse('desserts', 'Icecream', 5.99);
menu.addDishToCourse('desserts', 'Cookies', 5.99);
menu.addDishToCourse('desserts', 'Jello', 5.99);


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

this.appetizers.appetizer is not the dish objects name. It’s just the object at the appetizer index and don’t forget you need to use bracket notation when using a variable. Also your getRandomDishFromCourse is not returning a ‘dish’ object like the described function in step 10 of the project but it should still work correctly for now

2 Likes
generateRandomMeal: function() { //This is the problem.
   var appetizer = this.getRandomDishFromCourse('appetizers');
    var main = this.getRandomDishFromCourse('mains');
    var dessert = this.getRandomDishFromCourse('dessert');
    return 'Your meal is ' + this.appetizers.appetizer + ', ' + this.mains.main + ', and ' + this.desserts.dessert + '. The total price is $' +/* this.dish.price */ 8 + '.'; // I put the eight in place of the other thing for testing. I know it's a problem but I am getting there after this isue is solved.
  }

You need to ask yourself here, what does/should getRandomDishFromCourse return. And how does this relate to the message you a logging towards the receiver concerning the menu.

3 Likes

I see what you mean. Now I need to use this index but I can’t figure this out, I have tried:

this.appetizers[appetizer]

But this just logs Object object to the console.

You can think of it like this, at least this is the way I think about. this.appetizers is the array of dish objects that are appetizers. [appetizer] or more specifically appetizer is a number that represents the index in the array because your getRandomDishFromCourse method is returning a number instead of a dish object like the project instructed you to(Don’t really have to worry about it to get the code to work but it goes against the spirit of the lesson its trying to teach you). So you have the correct array and the location of the dish object you want, but now you have to access the dish object’s name key.

If you want the line that will work for you its under the spoiler below:

this.appetizers[appetizer].name

1 Like

Thank you, I will remember this.

1 Like