Project Meal Maker: Setter Methods and Arrays

Hi all,

I just finished the Meal Maker project and did the extra challenge of adding an array of meals and prices to randomly set and get Today’s Special. My final code is below.

My question is about the setter methods and arrays. In the project, we used the setter methods to ensure the assigned values of _meal and _price were strings and numbers respectively.

When I changed the values to arrays, I changed the typeof in the setter methods to “object” so I could access the elements in the array.

Is it possible to ensure the elements within an array are a specific data type?

Link to project Meal Maker

//generates random number between 0-2
let indexNum = Math.floor(Math.random() * 3);

const menu = {
 //values are empty so you can assign new values each day.
  _meal: "",
  _price: 0,
  set meal(mealToCheck) {
    //checks to see if the value of a meal is an object
    if (typeof mealToCheck === "object") {
      this._meal = mealToCheck;
    } else {
      console.log("not assigned correctly");
    }
  },
  set price(priceToCheck) {
    //checks to see if the value of the price is an object
    if (typeof priceToCheck === "object") {
      this._price = priceToCheck;
    } else {
      console.log("not assigned correctly");
    }
  },
  get todaysSpecial() {
    if (this._meal && this._price) {
      return `Today's Special is ${menu._meal[indexNum]} and it is $${menu._price[indexNum]}!`;
    } else {
      return `Meal or price was not set correctly!`;
    }
  },
};

//assigning the meal of the day using the setter methods
menu.meal = ["Chicken Parmigiana", "Lasagna", "Spaghett n' Meatballs"];
menu.price = [25, 24, 26];

menu.todaysSpecial;

console.log(menu.todaysSpecial);

That’s a very good question!
I’d try to use the array .every method to test every array element in the array to make sure it a specific data type. Not sure if that is the most efficient way especially for large data sets…

Thank you! I’ll give that a try and see how that works.

1 Like