Codecademy Forums

Meal maker: what about the setters?

I have a working solution, this is a more general question.

https://www.codecademy.com/paths/build-web-apps-with-react/tracks/bwa-javascript-iterators-objects-and-classes/modules/learn-javascript-objects/projects/meal-maker

I just finished a few hours on the meal maker exercise.

I ended up watching the walk through video and realised, THE SETTERS ARE NOT BEING USED in the official solution. Neither is the global courses getter.

I was struggling because I was trying to use the setters, and I still find no way of making it work by actually using the setters they ask to define in step 4.

Are they just dead code? Am I missing something??

.

1 Like

No. You’re not missing anything. Many learners have brought this up. Hopefully someday, they’ll modify this project to either use the setters or just leave them out. Happy coding!

3 Likes

Thank you for posting this! I thought the setters were being used (somehow) but I really didnt understand what the global courses getter was doing.

I have been also wondering about this; I got to the solution without using setters, and then modified it in order to get the results using them. I came up with the following:

  _courses: {
    appetizers: [], 
    mains: [], 
    desserts: []
  }, 
  get appetizers() {
    return this._courses.appetizers;
  }, 
  set appetizers(newAppetizer) {
    this.appetizers = newAppetizer;
  }, 
  get mains() {
    return this._courses.mains;
  }, 
  set mains(newMain) {
    this.mains = newMain;
  },
  get desserts() {
    return this._courses.desserts;
  }, 
  set desserts(newDessert) {
    this.desserts = newDessert
  }, 
  get courses () {
    return {
      appetizers: this.appetizers, 
      mains: this.mains, 
      desserts: this.desserts
    }
  }, 
  addDishToCourse (courseName, dishName, dishPrice) {
  const dish = {
    name: dishName, 
    price: dishPrice
  }
    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 consists of  \n${appetizer.name}\n${main.name}\n${dessert.name}\nThe total price of your meal is $${totalPrice}`;
  }
};
menu.addDishToCourse('appetizers', 'Nachos', 6);
menu.addDishToCourse('appetizers', 'Fried Pickles', 7);
menu.addDishToCourse('appetizers', 'Tomato Soup', 6);
menu.addDishToCourse('mains', 'Rib Eye Steak', 18);
menu.addDishToCourse('mains', 'Pepperoni Pizza', 18);
menu.addDishToCourse('mains', 'Chicken Sandwich', 12);
menu.addDishToCourse('desserts', 'Chocolate Ice Cream', 4);
menu.addDishToCourse('desserts', 'Flan', 5);
menu.addDishToCourse('desserts', 'Carrot Cake Slice', 6);
const meal = menu.generateRandomMeal();
console.log(meal);

I am getting the results, but I am not sure if I am doing it as expected. Please take a look at it and let me know what you see. Thanks

  set appetizers(newAppetizer) {
    this.appetizers = newAppetizer;
  },

The above setter will replace the array with a single value. Not something we would want.

  set appetizers(appetizer) {
    this.appetizers.push(appetizer);
  },

Same would apply to the mains and desserts setters.

Now to get the setters into the picture…

this.courses[courseName] = dish;
1 Like

I fixed each setter and the code is still working (Thanks!), but when I modify addDishToCourse in order to get the setters your way, it is giving me an error:

 addDishToCourse (courseName, dishName, dishPrice) {
  const dish = {
    name: dishName, 
    price: dishPrice
  };
    this.courses[courseName] = dish;
},

Is there something else I need to fix?

1 Like

Try using the backing variable. We don’t want the getter in this instance.

this._courses...
1 Like

Sorry for replying so late… So you think it is better in this case to not use the getter? If so, why? The project does suggest trying to use them…

The courses getter is rather complex, having three bindings, not one.When adding a single course we need to dial it in, hence direct access to the backing variable (the actual property) and the given attribute (courseName).

Above we still need getters and setters for the three course properties. Well, need? or want? is more the question.

I’m still a little uninformed in terms of how the getter interacts with the setter, and how the format we give the return value affects the setter. Need to devise some experiments (or search for them) to test.

One notion I have is that the getter creates a binding to the setter, which keeps it from binding to anything else while the setting takes place. The mutation is effected and the binding is removed. What this suggests to me is that we could have a getter format to any extent and it would not affect the setter since it is only the binding that is tied up, and the return value is not considered.

Perhaps someone better informed in all this will pipe in to set us straight by confirming or correcting these notions.

1 Like

I was thinking that, at least in this project, they are trying to get us to use getters and setters in order to leave alone the backing variables. I am also trying to figure out how do they interact with each other, when should they be used, and what is the correct way to do so. I will leave it for now and go on, I hope someone sees this and answers our questions

1 Like

On rethinking, one thinks the setter creates the binding with the getter, which stirs the interrupt allowing the mutation to take place.

1 Like

That is something I have thought about before, but then I can’t decide how exactly to interpret the logic. I hope the answer reveals itself later. Sorry for the very late reply, by the way; I got tangled up with work

1 Like