How does making this global scope function local make it work better?

Hi! In the race day project I am confused as to how moving the randomzing constant into the functin (step 11) made it work better? It was global at first, so why does making it local scoped make it work?
Here is the link to the project.


When the line const random = Math.floor(Math.random() * 3); is in the global scope, it is executed only once at application startup. This way you always use the same random value, and each call to getRandEvent will return the same event, not a random one as the function name suggests. Moving this line into the getRandEvent function will cause it to be called every time the function is executed. This way you will get a random number (and random event) every getRandEvent call without reloading the app.

1 Like

Making local scopes work better because global scopes can cause errors in your code and can make your code look messy. Happy to help

1 Like

This makes a lot of sense! Thank you so much for your response!

Oh, yeah this makes sense, too. Thanks!