Meal Maker Project Step 12

Hello, I recently completed the meal maker project and decided to give the additional challenge described in step 12 an attempt: adding an array of meals and prices to randomly set and get Today’s Special. I think everything is in working order but was contemplating if this code could be more condense and simplified using certain types of methods? Here is the code below:

// Random Meal Maker Step 12

const arrMeal = [‘Burger and Chips’, ‘Pizza’, ‘Pasta’, ‘Shrimp Noodles’];

const arrPrice = [‘Burger and Chips’, 5, ‘Pizza’, 15, ‘Pasta’, 7, ‘Shrimp Noodles’, 12];

const menu = {

_meal: ‘’,

_price: 0,

set meal(mealToCheck){

if(typeof mealToCheck === ‘string’){

return this._meal = mealToCheck;

}

},

set price(priceToCheck){

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 and price incorrectly set!';

}

},

randomMealSelector(){

const randomIndex = Math.floor(Math.random() * arrMeal.length);

return arrMeal[randomIndex];

},

determinePrice (){

for(let i = 0; i < arrPrice.length; i++){

  if(arrPrice[i] === this._meal){

    return arrPrice[i + 1];

  }

}

}

}

menu.meal = menu.randomMealSelector();

menu.price = menu.determinePrice();

console.log(menu.todaysSpecial);

console.log(menu);

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.