So I am currently working through some code academy problems and was thinking about the "recommended" solution from them. The code is as follows:
...
getRandomDishFromCourse(courseName) {
...
return this._courses['appetizers'];
},
generateRandomMeal() {
const appetizer = this.getRandomDishFromCourse('appetizers');
...
return `Your meal is ${appetizer.name}...`
}
So they call the getRandomDishFromCourse with a string as an argument and then in the function I access the object through the bracket notation. How would I solve this with getters and setters instead? What is the best practice for this?
My idea for solving this is presented below, but does not work...
get appetizers() {
return this._courses._appetizers;
},
...
getRandomDishFromCourse(courseName) {
let dish = courseName;
return dish;
...
},
generateRandomMeal() {
const appetizer = this.getRandomDishFromCourse(this.appetizers);
...
return `Your meal is ${appetizer.name}...`
}
setforappetizersaccessors is incorrect: It doesn't setappetizers, it adds toappetizers. Just adding to the array isn't a use-case for a setter.