Hi everyone, I was struggling with the Meal Maker project until I realized my problem was that I had inconsistently written things as ‘appetizers’ and ‘appetizer’ throughout the code. My function then worked, but it left me wondering:
Why does our key need to be a string in order to access it? Is this an intrinsic way that objects work and I have totally forgotten?
generateRandomMeal() {
const appetizer = this.getRandomDishFromCourse('appetizers');
const main = this.getRandomDishFromCourse('mains');
const dessert = this.getRandomDishFromCourse('desserts');
const totalPrice = appetizer.price + main.price + dessert.price;
console.log(`Your meal is ${appetizer.name}, ${main.name}, and ${dessert.name}. That will be $${totalPrice}.`);
}
};
VS
generateRandomMeal() {
const appetizer = this.getRandomDishFromCourse(appetizers);
const main = this.getRandomDishFromCourse(mains);
const dessert = this.getRandomDishFromCourse(desserts);
const totalPrice = appetizer.price + main.price + dessert.price;
console.log(`Your meal is ${appetizer.name}, ${main.name}, and ${dessert.name}. That will be $${totalPrice}.`);
}
};
(The difference is I took the ’ ’ off of the words appetizers, mains, desserts, etc.)
Similarly when accessing it from getRandomDishFromCourse(), why do we declare
getRandomDishFromCourse(courseName) {
const dishes = this._courses[courseName];
const randomIndex = Math.floor(Math.random() * dishes.length);
return dishes[randomIndex];
},
instead of
const dishes = this._courses.courseName;
?
Any help would be appreciated.
Full code in details if it helps.
Summary
const 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(appetizerIn) {
this._courses.appetizers = appetizerIn;
},
set mains(mainsIn) {
this._courses.mains = mainsIn;
},
set desserts(dessertsIn) {
this._courses.desserts = dessertsIn;
},
get courses() {
return _courses;
},
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;
console.log(`Your meal is ${appetizer.name}, ${main.name}, and ${dessert.name}. That will be $${totalPrice}.`);
}
};
menu.addDishToCourse('appetizers', 'jalapeno poppers', 5);
menu.addDishToCourse('appetizers', 'onion rings', 4);
menu.addDishToCourse('appetizers', 'freedom fries', 3);
menu.addDishToCourse('mains', 'steak', 16);
menu.addDishToCourse('mains', 'borger', 13);
menu.addDishToCourse('mains', 'salads', 10);
menu.addDishToCourse('desserts', 'shake', 5);
menu.addDishToCourse('desserts', 'cake', 6);
menu.addDishToCourse('desserts', 'ice cream', 4);
const meal = menu.generateRandomMeal();
console.log(meal);