Meal Maker, new elements with .push() don't show up as additional random option?

I’m not sure if I forgot something or if I didn’t properly understand how arrays in objects work, but anyways:
I have a working code that gets me a random meal for a random price. When I add new meals with .push() to the array in the menu object, it updates the array length and shows the meals that I added when I log the array.
I ran the code several times, but despite this I only get the first 8 meals that I had written in the array? I thought ok, this could be a freak chance that the new Items don’t display, so I added more to increase the likehood of one of the pushed meals to show up, but no. Only the original 8.

What do I have to change or add so newly added meals also get randomly selected and logged?

const menu = { meals: [ "Shepheard Pie", "Pizza","Pasta", "Kebab","Cold Glassnoodles Salad","Oyakodon","Japanese Curry","Spaghetti Carbonara"], prices: [5, 7, 10, 12, 15, 20], randomMath(arr) { const randomIndex = (Math.floor(Math.random() * arr.length)) const item = arr[randomIndex] return item }, get randomMeal() { return this.randomMath(this.meals) }, get randomPrice() { return this.randomMath(this.prices) }, get todaysSpecial() { return "Today's Special is " + this.randomMeal + " for only " + this.randomPrice + "$! Enjoy!" } } console.log(menu.todaysSpecial) //OMG IT WORKS????? WHAT???? menu.meals.push("Momo's", "Sandwich", "Burger", "the Rich", "Fries", "Ramen", "IDK", "uuuuuh") console.log(menu.meals.length) //oh yey push works uwu console.log(menu.meals) //whyyyyyyy all the elements are here why are you doing this to meeeeee plssss

Every time your code runs, the last console logs the initial 8 items plus the ones you added. If you want to see one of the newly added items as a random output of todaysSpecial, you need to run the console with menu.todaysSpecial below the push because every time you run your code it runs from top to bottom with the new items available only after you pushed them.

2 Likes

Oh my god, that’s it!!!
Thank you so so much, I can’t believe I was so stuck within this that I didn’t see this simple mistake. You saved so many of my nerves. :sob: :hearts: :hearts: :hearts:

1 Like