Meal maker

Hello could someone please help me out with whats wrong on this code, thanks in advance…

https://gist.github.com/ffd1510994befbb60c6387d8ff91d818strong text

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:48
const randomIndex = Math.floor(Math.random() * dishes.length);
^

TypeError: Cannot read property ‘length’ of undefined
at Object.getRandomDishFromCourse (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:48:58)
at Object.generateRandomMeal (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:53:10)
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:71:17)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)

const menu = {
_courses: {
appetizers: ,
mains: ,
desserts: ,
},

get appetizers() {
return this._courses.appetizers;
},
set appetizers(app) {
this._courses.appetizers = app;
},

get mains() {
return this._courses.mains;
},
set mains(entree) {
this._courses.mains = entree;
},

get desserts() {
return this._courses.desserts;
},
set desserts(sweets) {
this._courses.desserts = sweets;
},

get courses() {
return {
appetizers: this.appetizers,
mains: this.mains,
desserts: this.desserts
};
},

addDishToCourse(courseName, dishName, dishPrice) {
const dish = {
name: dishName,
price: dishPrice
};

  this._courses[courseName].push(dish);
  },

getRandomDishFromCourse(courseName) {
const dishes = this._courses[courseName];
const randomIndex = Math.floor(Math.random() * dishes.length);
},

generateRandomMeal() {
let appetizer =
this.getRandomDishFromCourse(‘Appetizer’);
let mains = this.getRandomDishFromCourse(‘main’);
let desserts =
this.getRandomDishFronCourse(‘dessert’);
const totalPrice = appetizer.price + mains.price + desserts.price;

return 'Your meal is ' + appetizer.name + main.name + '. The price is $' + totalprice + '.';

},
};

menu.addDishToCourse(‘appetizers’, ‘caesar salad’, 5);
menu.addDishToCourse(‘mains’, ‘aguacate’, 45);
menu.addDishToCourse(‘desserts’, ‘tecate’, 100);

console.log(menu._courses);

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

If you analyze the error that it’s showing, you will see that the dishes variable inside your getRandomDishFromCourse() is coming out as undefined. It couldn’t find any course with the given names.

So, look closely at what name you gave each course when you defined them at the top of your code. Then, compare those names with the names you are passing as arguments when you call your getRandomDishFromCourse() function. Notice any difference?

2 Likes

thank you for your help. I finally got it done, now I am just wondering what is the purpose of our setters in this code.

I have fixed already the parameters in the getters also, but I don’t see where does parameters yield their input from.

You’re welcome, glad you could get it done :blush:

To be completely honest, I’m not sure. I myself didn’t use the setters when working on this project. I’m guessing it’s just for the sake of practicing (?).

Also I’m not sure I understood your last question. (It could be because of my not-so-good English…Spanish is my first language). Would you mind rephrasing it?

it has to do with the setter for example :

set appetizers(appetizersIn) {

},

In here the setter has a parameter of (appetizersIn)

in our code that parameter goes in to the getter :

get courses () {

},

I am confused how is the parameter (appetizersIn) is used in the code when we never send any arguments to it.

The getCourses() function is actually using your getters, not your setters.
The getters do not take any parameters, so that’s why we’re not giving it any arguments and it still works.

(I hope I understood your question correctly, feel free to correct me if I didn’t).