Another Meal Plan

I enjoyed doing this project because it required a simple and achievable task for me as a beginner.

I hope I did not miss any requirements, but I tried to make it simple and workable if we have more meal options included in the arrays.

repo link https://github.com/gacarneirojr/random-diet

Thanks for your feedback!

Maybe I am thinking to complicated, but this:

    let random1 = Math.floor(Math.random()*breakfast.length);
    let random2 = Math.floor(Math.random()*lunch.length);
    let random3 = Math.floor(Math.random()*dinner.length);

feels repetitive, you have 3 times the same thing with a slight variation. Feels like this could be abstracted into a separate function.

in fact, I would make that function in such a way that a random meal is returned, not a random integer.

1 Like

I got you! I tried to put everything together in a single function. But you are right! too many variables.

I’ll try to change in less code for the same result.

I appreciate your feedback!

1 Like

and too much of the same code/logic.

Its okay to put everything in one function will writing, but then after you are done should look at it: Is this good? Can this be done better?

I have changed. What do you think?

I think I would personally do this:

return meal[Math.floor(Math.random()*meal.length)];

I mentioned this briefly in my first reply:

which eliminates some more repetitiveness.

then you have a single line/body function, which you can short-hand if you like:

const randomMeal = meal => meal[Math.floor(Math.random()*meal.length)];
1 Like

I have done it and really looks better!

thank you, man!

1 Like