Hello everyone, I just completed this project while working on the Full Stack Engineer career path. It’s called the Meal Maker project and I was supposed to make a menu object that would return today’s special randomly from an array of meals and prices. at first I tried to have this random logic in the object itself but I couldn’t find a way to do it so I just put it outside. Is there a way I could put the random logic within the object and have it do the same thing? Thank you for your help!
let meals = ['filet mignon', 'tacos', 'snails', 'pad thai', 'sushi'];
let prices = [28, 9, 24, 15, 12];
let randMealIndex = Math.floor(Math.random() * meals.length);
const menu = {
randomMeal: meals[randMealIndex],
mealPrice: prices[randMealIndex],
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.randomMeal && this.mealPrice) {
return `Today\'s special is ${this.randomMeal} for $${this.mealPrice}!`;
} else {
return 'Meal and/or price was not set correctly!';
}
}
}
console.log(menu.todaysSpecial);