What's the point of setters in the intro meal maker project?

The instructions ask to use two set functions to type check both the meal and price inputs, however I was able to achieve the same result by type checking directly within the “get todaysSpecial()” function as follows:

get todaysSpecial () {
  if (typeof this._meal === 'string' && typeof this._price === 'number') {
    return `Today's Meal is ${this._meal} for ${this._price}!`
  } else {
    return 'Meal or price was not set correctly.'
  }
}

};


menu._meal = 'pizza';
menu._price = 10;

console.log(menu.todaysSpecial);

Is there another “function” of the set functions in this exercise? Is there any problem with just type checking within the get function? Why use setters in the this instance when you can check values’ existence and type in one getter block?

Design specifications. They are either in the spec, or they are not. Given the syntactic sugar that comes in tow, don’t treat them as an albatross. They’re actually useful or more ways than one.

As noted by @mtf it was in the spec. Why might it be in the spec? Well, imagine that there might be other functions, possibly in future versions of the app that use the name and price of a meal. The use of a setter to ensure that the price and name are of the correct type and are valid values prevents the need to validate it in every other method where they are used. Ultimately both versions work, but the way the course lays it out will allow you to more easily extend and reuse code.

1 Like