Introduction to Java Script course - Meal Maker project issue with console.log(meal) undefined

Hi,

Here is the code I’ve been working on for this project:

const menu = {
generateRandomMeal () {
const appetizer = this.getRandomDishFromCourse(‘appetizers’);
const main = this.getRandomDishFromCourse(‘mains’);
const dessert = this.getRandomDishFromCourse(‘desserts’);
let totalPrice = appetizer.price + main.price + dessert.price;
console.log(Your meal consisted of ${appetizer.name}, ${main.name}, ${dessert.name} which costed ${totalPrice});
},
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.length);
return dishes[randomIndex];
},
_courses: {
appetizers: ,
mains: ,
desserts:
},
get appetizers () {
return this._courses.appetizers;
},
set appetizers (appetizersIn) {
this._courses.appetizers = appetizersIn;
},
get main () {
return this._courses.main;
},
set main (mainIn) {
this._courses.main = mainIn;
},
get desserts () {
return this._courses.desserts;
},
set desserts (dessertsIn) {
this._courses.desserts = dessertsIn;
},
get courses () {
return _courses;
}
};
menu.addDishToCourse(‘appetizers’, ‘bread sticks’, 5);
menu.addDishToCourse(‘appetizers’, ‘salad’, 2);
menu.addDishToCourse(‘appetizers’, ‘wings’, 10);

menu.addDishToCourse(‘mains’, ‘stake’, 25);
menu.addDishToCourse(‘mains’, ‘pasta’, 15.50);
menu.addDishToCourse(‘mains’, ‘pizza’, 12.30);

menu.addDishToCourse(‘desserts’, ‘cake’, 13.60);
menu.addDishToCourse(‘desserts’, ‘ice cream’, 10.50);
menu.addDishToCourse(‘desserts’, ‘pudding’, 8.30);

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

Can somebody tell me two things:

  1. Why would I need consol.log(meal) at the end when menu.generateRandomMeal(); already prints comment and results?
  2. Why console.log(meal); returns “undefined” ?

Thanks!

Hey,

Console.log(meal) returns an “undefined” because your generateRandomMeal() does not return an output. In this case generateRandomMeal() logs the meal to the console and not your console.log(meal) call.

Instead you would want the generateRandomMeal() to return that what console.log(meal) should print (this is step 11).

i.e.

generateRandomMeal() {
    const appetizer = this.getRandomDishFromCourse('appetizers');
    const main = this.getRandomDishFromCourse('mains');
    const dessert = this.getRandomDishFromCourse('desserts');
    const totalPrice = appetizer.price + main.price + dessert.price;
    
    return `Your meal is ${appetizer.name}, ${main.name} and ${dessert.name}. The price is $${totalPrice}.`;
  }
};

This way both your remarks are solved ;).

2 Likes

@predragaranelovi7027 You are welcome :wink: