Meal Maker Project


#1

Hello, does anyone know how to use the set method to add dishes to the courses object in the Meal Maker project?

Here’s what I’ve got. Thanks for any help.

const menu = {
_courses: {
_appetizers: [],
_mains: [],
_desserts: [],

get appetizers() {
  return this._appetizers;
},

set appetizers(newAppetizer) {
  this._appetizers.push(newAppetizer);
  this._appetizer = newAppetizer;
},
get mains() {
  return this._mains;
},
set mains(newMain) {
  this._mains.push(newMain);
},
get desserts() {
  return this._desserts;
},
set desserts(dessertsIn) {
  this._desserts.push(newDessert);
},

},

get courses() {
return {
appertizers: this._courses._appertizers,
mains: this._courses._mains,
desserts: this._courses._desserts
}
},

addDishToCourse(courseName, dishName, dishPrice) {
const dish = {
name: dishName,
price: dishPrice
}
this._courses[courseName].push(dish);
/How to use set method?/
},

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

generateRandomMeal() {
let appetizer = this.getRandomDishFromCourse(‘appetizers’);
let main = this.getRandomDishFromCourse(‘mains’);
let dessert = this.getRandomDishFromCourse(‘desserts’);
const totalPrice = appetizer.price + main.price + dessert.price
return "Your meal is appetizer: " + appetizer.name+ ", main: " +main.name+ ", and dessert: " + dessert.name+ “.”;
}

};

menu.addDishToCourse(‘appetizers’, ‘Ceasar Salad’, 4.25);
menu.addDishToCourse(‘appetizers’, ‘Prawn Coctail’, 4.25);
menu.addDishToCourse(‘appetizers’, ‘Garlic Bread’, 3.50);

menu.addDishToCourse(‘mains’, ‘Lasagna’, 9.75);
menu.addDishToCourse(‘mains’, ‘Ribeye Steak’, 14.95);
menu.addDishToCourse(‘mains’, ‘Fish & Chips’, 12.95);

menu.addDishToCourse(‘desserts’, ‘Cheese Cake’, 4.50);
menu.addDishToCourse(‘desserts’, ‘Creme Brule’, 4.25);
menu.addDishToCourse(‘desserts’, ‘Cheese Board’, 3.25);

console.log(menu._courses._appetizers);
console.log(menu._courses._mains);
console.log(menu._courses._desserts);

console.log(menu.getRandomDishFromCourse(‘appetizers’));

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


#2

That line can go. it has no place in the setter, especially given that this._appetizer does not exist.

We don’t add dishes to this object, but to the objects it contains, namely, appetizers, mains, and desserts, which all have their own setter.

Adding a new course will involve some careful thought to implement. That is a challenge for when you have this module completed.


#3

Thanks for your reply, that line is there by mistake. I was testing things out and forgot to delete it. I’ve finished the objects module but am struggling with the idea of getters and setters. Why would I use a set method to push an element to the array, when I could do that with something like the addDishToCourse function.


#4

Setters are a convenience tool. They let us bridge the gap between ._prop and .prop.

this._prop = prop;

followed by,

set prop(newProp) {
    this._prop = newProp;
}

Setters are not functions in the classical sense. They are class methods whose inputs are directly declared, rather than passed as arguments.

instance.prop = "new value";

Once we get over this hurdle, getters are a breeze.

get prop() {
    return this._prop;
}

instance.prop    // 'new value'

We can also take advantage of the getter inside the class,

repr() {
    return `prop: ${this.prop}`;
}

console.log(instance.repr())    // prop: 'new value'

It takes a minute to get your head around this, but it will be time well spent. Don’t skip this unit. Stay with it until you can do this in your sleep (and might well be doing as you study it).


#5

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