Hello, campers!
I’m using multiple online resources to learn JavaScript, and both FCC and codecademy have finally introduced me to the loop. I’ve decided to try out a mini-project that involves a loop.
On Sunday nights, I like to plan out what I’m going to be eating for lunch that week. Anal, I know, but I like to just grab a lunch and run out the door for work in the morning rather than waste money on takeout or unhealthy fast food everyday. So, I decided to create a system to randomly choose what I’m eating for lunch Monday-Friday based on the meals I have in my fridge.
I have the meals variable, an array of what’s in the fridge.
The weekday variable is an array of Monday through Friday.
And the last variable I can’t figure out how to incorporate into my loop is the weekdayLunch variable, the value of which is randomly selected from the meals array.
var meals = ["PB + J", "spinach salad with tofu", "granola and yogurt", "tofurkey sandwich", "leftover chili", "lentil soup", "breakfast burrito", "protein cookie"];
var weekday = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"];
var weekdayLunch = (Math.floor(Math.random) * meals.length);
for (var i = 0; i <= 4; i++ ) {
console.log(weekday[i] +"'s lunch will be " + weekdayLunch);
}
Here is what the above code returns:
Monday's lunch will be NaN
Tuesday's lunch will be NaN
Wednesday's lunch will be NaN
Thursday's lunch will be NaN
Friday's lunch will be NaN
Why is NaN coming up? Rather than NaN, I want it to be a random selection from the array defined in “meals”. Is this a simple problem I’m overlooking, or does my code require more of an overhaul?
Thanks for reading!
-Julianna