Meal Maker - getters and setters

no, naming conventions like how the property (with underscore) and getters and setters are named are well thought through.

you would need a very good reason to go against it

give the getter and setter are sort of magic properties, they should have the same name.

1 Like

const menu = {
_courses: {
appetizers: ,
mains: ,
desserts: ,
},
get appetizers() {
return this._courses.appetizers;
},
get mains() {
return this._courses.appetizers;
},
get desserts() {
return this._courses.appetizers;
},
set appetizers(appetizers) {
this._courses.appetizers = appetizers;
},
set mains(mains) {
this._courses.mains = mains;
},
set desserts(desserts) {
this._courses.desserts = desserts;
},
get courses(){
return {
appetizers: this.appetizers,
mains: this.mains,
desserts: this.desserts,
};
},
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 appetizers = getRandomDishFromCourse(‘appetizers’);
const mains = getRandomDishFromCourse(‘main’);
const desserts = getRandomDishFromCourse(‘desserts’);
const totalPrice = appetizer.price + mains.price + desserts.price;
return Your meal ${appetizer.name}, ${mains.name},${desserts.name}, and your price is ${totalPrice};
}

};

menu.addDishToCourse(‘appetizers’,‘salad’, 14.50)
menu.addDishToCourse(‘appetizers’,‘fries’, 24.50)
menu.addDishToCourse(‘appetizers’,‘idli’, 48.50)

menu.addDishToCourse(‘mains’,‘roti’, 1.50)
menu.addDishToCourse(‘mains’,‘sabzi’, 9.50)
menu.addDishToCourse(‘mains’,‘dal’, 6.50)

menu.addDishToCourse(‘desserts’,‘cake’, 4.50)
menu.addDishToCourse(‘desserts’,‘macroon’, 3.50)
menu.addDishToCourse(‘desserts’,‘biscuits’, 8.50)

check = menu.getRandomDishFromCourse.dishes
console.log(check)

Why do i keep getting dishes is undefined.

dishes is not a property of getRandomDishFromCourse, which is not an object. It is a method.

At this point we should be calling getRandomMeal.

This was just to check ,
When i point to getrandommeal
i get an error saying dishes in getRandomDishFromCourse is undefined.
dishes.length doesnt work ,

Yes, it is undefined in global scope, but not in menu context.

... = this.getRandomDishFromCourse(%courseName%)

Got the problem , Appetizer , Appetizers

■■■■ it.
Thanks for your help , Was a spell error.

1 Like

Hi stetim94,

I’m another student confused about getters and setters. On your comment about the benefits of getter, can’t you still return a string without the getter? I think the join function is what makes the array into a string, not the getter.

Would it be fair to say that the real benefit of using getter and setter is that you can change the behavior of the property AND assign a value using one function, which can meaningfully improve efficiency if that function is used multiple times in a program whether to return or set values?

I also think CC needs to revamp this lesson!

Thank you so much for your help on this forum!

You could, but now we have a mutable property (array) so we can do easy manipulation but give the output only as string. This means other developers looking at your code can know the restrictions in your program

getters and setters can also be used for validation for example, so you can dictate certain behavior.

Part of programming is to write understandable code, getters and setters can help with this

1 Like

Thanks stetim94 for a quick response! I recognize that getter signals to other developers restrictions to my program, but I don’t fully understand the below point:

“but now we have a mutable property (array) so we can do easy manipulation but give the output only as string.”

Even if you remove “get,” doesn’t the code (“Function without Getter”) work the same way and you still have a mutable property? One inefficiency that may arise from this is that you now will have to name the function in the setter, should you decide to use the setter, differently from the one used in Function without Getter.

Please let me know what I’m missing here!

true, but no longer restrict the the output to a string. Instead, you will get the array

properties which have getters and setters should not be accessed directly (would be nice if JavaScript actual enforced encapsulation)

also, this is just one example. I think getters and setters become more useful (as well as other abstractions) if your code base starts to grow, so its okay if you struggle the weigh the benefit/drawback of each approach. This is something you will learn with experience

It does, but it requires a closure that returns a function, if I’m not mistaken.

Thanks, stetim94. Just so I’m clear, the code below will return an array, not a string? Doesn’t join() stringfy the array?

On your last point, that’s what I have been reading from other sources as well.

Thank you so much for your response!

myObj = {
  _myArray: [],
  myArray(){
    return this._myArray.join(' ');
  },
  set myArray(element){
    this._myArray.push(element);
  }
}

if you do:

myObj = {
  _myArray: [],
  myArray(){
    return this._myArray.join(' ');
  },
  set myArray(element){
    this._myArray.push(element);
  }
}

myObj.myArray = 5;
console.log(myObj._myArray);

then yes, you get an array. You could have simply tested this? :slight_smile: Trying to figure things out on your own is always a good idea

but this code has other problems, its impossible call the myArray method, given the setter will create a property with the same name