Meal Maker Project

Hi, I’m trying to complete the following project by doing something “extra”:

But I don’t quite understand how to pass just 1 random element into the “getter” part of the object instead of it passing the entire array. Can someone, please, explain what I’m doing wrong?

const menu = {
_meal: ['Pineapple Pizza', 'Chicken Ramen', 'Coconut Curry', 'Carbonara', 'Just a Glass of Water'],
_price: [0.99, 999, 15, 2, 50, 7.50, 8],

set meal(mealToCheck){
mealToCheck = this._meal[Math.floor(Math.random() * this._meal.length)];
  if (typeof mealToCheck === 'string') {
    return this._meal = mealToCheck;

set price(priceToCheck) {
priceToCheck = this._price[Math.floor(Math.random() * this._price.length)];
  if (typeof priceToCheck === 'number') {
    return this._price = priceToCheck;

get todaysSpecial() {

  if (this._meal && this._price) {
    return `Today's Special is ${this._meal} for $${this._price}!`
  } else {
    return 'Meal or price was not set correctly!'


Math.floor(Math.random() * this._meal.length) gives you a random integer (whole number) from 0 to one less than the length of of the this._meal array
(which would be an number you could use as an index to access stuff in this._meal)
this._meal[Math.floor(Math.random() * this._meal.length)]; gets a random meal (because it gets something from the this._meal array at a random index).
this._price[Math.floor(Math.random() * this._price.length)];
gets a random price.

You have
mealToCheck = this._meal[Math.floor(Math.random() * this._meal.length)];
in the wrong function/setter.
It should not be in set meal(mealToCheck) function because that line would overwrite the mealToCheck variable.
Also, the purpose of the meal setter is to possibly assign something to this._meal, not to generate a random meal.
(More specifically, that setter is meant to change the value of this._meal if mealToCheck is a string;
where mealToCheck is the “value” being assigned to menu.meal ).

priceToCheck = this._price[Math.floor(Math.random() * this._price.length)];
does not belong in the price getter.

Getting a random meal and its price belongs in the todaysSpecial() method
since the purpose of that is to set the ._price, not to get a random price.

You could have something like the following code in the beginning of the todaysSpecial method

  const index = Math.floor(Math.random() * this._meal.length); // gets a random index
  const randomMeal = this._meal[index]; // the special is a random meal (the meal at the random index)
  const priceOfMeal = this._price[index]; // price of the special
1 Like

Thanks for your explanation, it was really helpful! :blush: