[英]broken code at .push in JS.
I cant seem to find why I cannot push my dish to courses[courseName] in the addDishToCourse method.. courseName is an array, so there shouldn't be any issues (although, factually, that't not true ><). 我似乎无法找到为什么不能在addDishToCourse方法中将菜式推送到Course [courseName]的原因。courseName是一个数组,因此应该没有任何问题(尽管事实上,这不是真的> <)。 Please, help? 请帮忙?
const menu = {
_courses : {
_appetizers : [],
_mains : [],
_desserts : []
},
>> set appetizers(appetizerIn) {
},
get appetizers() {
},
set mains(mainIn) {
},
get mains() {
},
set desserts(dessertIn) {
},
get desserts() {
},
get courses() {
return {
appetizers : this._courses.appetizers,
mains : this._courses.mains,
desserts : this._courses.desserts
}
},
//Below is where my code breaks with the .push
addDishToCourse(courseName, dishName, dishPrice) {
let dish = {
name : dishName,
price : dishPrice
};
this._courses[courseName].push(dish);
},
getRandomDishFromCourse(courseName) {
const dishes = this._courses[courseName];
const randomIndex = Math.floor(Math.random() * this.dishes.length);
return dishes[randomIndex];
},
generateRandomMeal() {
const appetizer = this.getRandomDishFromCourse('appetizers');
const main = this.getRandomDishFromCourse('mains');
const dessert = this.getRandomDishFromCourse('desserts');
//const totalPrice = appetizers.price + mains.price + desserts.price;
return `Your appetizer is ${appetizers.name} followed by the main meal, which will be ${mains.name}, and finally you will have ${desserts.name} for dessert.`;// Your bill will be of ${totalPrice}.`;
}
};
menu.addDishToCourse('appetizers', 'Caesar Salad', 3.75);
menu.addDishToCourse('appetizers', 'Srimp Cocktail', 6.50);
menu.addDishToCourse('appetizers', 'Escargots Gratines', 4.50);
menu.addDishToCourse('mains', '16oz Ribeye', 38);
menu.addDishToCourse('mains', 'Smoked Salmon', 18);
menu.addDishToCourse('mains', 'Grilled Chicken Breast', 19);
menu.addDishToCourse('desserts', 'Chocolate Lava Cake', 3.50);
menu.addDishToCourse('desserts', 'Tiramisu', 4);
menu.addDishToCourse('desserts', 'Kiev Cake', 6.50);
let meal = menu.generateRandomMeal();
console.log(meal);
Now I tried using an if statement to get around the .push, but that just broke the code elsewhere. 现在,我尝试使用if语句来绕过.push,但这只是破坏了其他地方的代码。 Otherwise, I know that I potentially use a setter method, but I'm not too sure on how to deal with that either. 否则,我知道我可能会使用setter方法,但是我也不十分确定该如何处理。 Would I set the addDishToCourse? 我会设置addDishToCourse吗?
I am a novice (as seen by the simplistic code^_^) so any help would be greatly appreciated! 我是新手(通过简单代码^ _ ^可以看到),所以任何帮助将不胜感激!
When you create your new instance your courseName
needs to match the name of the key in your _courses
object. 创建新实例时,您的courseName
需要与_courses
对象中的键名匹配。 In your code all of these names are preceded by an underscore so the code breaks. 在您的代码中,所有这些名称都带有下划线,因此代码会中断。
For example: 例如:
menu.addDishToCourse('appetizers', 'Caesar Salad', 3.75);
You're passing in "appetizers" as the course name... 您正在传递“开胃菜”作为课程名称...
addDishToCourse(courseName, dishName, dishPrice) {
let dish = {
name : dishName,
price : dishPrice
};
this._courses[courseName].push(dish);
},
...and you're trying to add "appetizers" to _courses
, but _courses
only has the _appetizers
key name. ...并且您正在尝试向_courses
添加“开胃菜”,但是_courses
仅具有_appetizers
键名。
I would change the name of your object keys to appetizer
, mains
, and dessert
respectively. 我可以将对象键的名称分别更改为appetizer
, mains
和dessert
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.