Meal Maker

Would someone be able to explain this part of the code to me please? Is it just picking one out of the three appetizers, mains, desserts, each? How does it know to pick one out of those three options each? Please explain with as much detail as possible as the video walkthrough doesn’t really explain it.

https://www.codecademy.com/courses/introduction-to-javascript/projects/meal-maker

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

Really appreciate the help, thanks!

Here’s the full code:
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.mains = 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.length);
return dishes[randomIndex];
},
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}, and the total price is ${totalPrice};
}
};

menu.addDishToCourse(‘appetizers’, ‘salad’, 5.50)
menu.addDishToCourse(‘appetizers’, ‘wings’, 8.25)
menu.addDishToCourse(‘appetizers’, ‘artichoke dip’, 7.75)

menu.addDishToCourse(‘mains’, ‘meatloaf’, 15.85)
menu.addDishToCourse(‘mains’, ‘salmon’, 21.99)
menu.addDishToCourse(‘mains’, ‘pasta’, 14.50)

menu.addDishToCourse(‘desserts’, ‘flan’, 7.25)
menu.addDishToCourse(‘desserts’, ‘cheesecake’, 8.75)
menu.addDishToCourse(‘desserts’, ‘ice cream’, 6.35)

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

Why, is there something in it you don’t know what it does? If so, what? Seems to me like you should learn the individual parts before you consider the whole, and if you know the individual parts then you know the whole as well.

“explain this code” …the code says what it does. that’s what code does. that question makes me wonder what you are actually asking, because it’s surely not that.

you might for starters consider what the purpose of the function is, that’ll tell you much if not all of what it does

Thanks for the quick response. I edited my questions at the top.

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

So later on when I call the function:
menu.addDishToCourse(‘appetizers’, ‘salad’, 5.50)

dishes.length would become the number of characters in ‘appetizers’? Or would dishes.length equal the 3 items in (‘appetizers’, ‘salad’, 5.50)?

And then it would be multiplied by a random number, and then I don’t understand why it’s returning a [randomIndex] or what that [randomIndex] is for. I assume the [randomIndex] would inform which of the

menu.addDishToCourse(‘appetizers’, ‘salad’, 5.50)
menu.addDishToCourse(‘appetizers’, ‘wings’, 8.25)
menu.addDishToCourse(‘appetizers’, ‘artichoke dip’, 7.75)

are chosen later on but I’m not connecting the dots.

Thanks so much for the help!

Well what type is it? Presumably it’s either an array or an object. How do those behave? What does the size of an array tell you about its contents?
You can print it too. You can print out things before and after you carry out an operation to see what effect it has. You can modify the operation and run it again to see how the outcome changes. You can consider what operation is being used and read the documentation for that operation. You can make a simpler program that only contains the operation that you want to look at, so that nothing else is interfering. You can add code at the bottom of your current code which does different things to your menu._courses

console.log(typeof menu._courses)
console.log(menu._courses)

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