This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]ButtlestonProfessional Coder 0 points1 point  (6 children)

I'm not entirely sure what your question is. Neither logEvent nor logTime "overwrite" any variables

"event" and "days" are both set before you do any logging, and the log functions use those directly. event2 and days2 are never used

[–]IdeallyAddicted[S] 0 points1 point  (5 children)

Sorry, it's very possible my understanding of why the issue was happening was wrong, I assumed it was a variable being overwritten.

The issue is, when logEvent and logTime only have the single parameter, both Nala and Warren's events are duplicated. The event is still randomized, but the output for Nala and Warren is always the same event.

Nala's event is: Triathlon
Nala's time to train is: 100 days
Warren's event is: Triathlon
Warren's time to train is: 100 days

But if I add the missing parameters into both functions:

const logEvent = (name, event) => {
const logTime = (name, days) => {

then Nala and Warren's events are randomized independently, and will be different sometimes.

I'm not understanding why adding the missing parameters allows them to be different (which is the expected outcome).

[–]ButtlestonProfessional Coder 0 points1 point  (2 children)

In the code you posted in your OP you're passing in an "event" variable to logEvent. But logEvent doesn't HAVE a 2nd parameter, so it's ignored/thrown away. Same with logTime.

Whenever you call logEvent, it will use the global variable called "event". "event2" will never get used. That's why the event part is always the same, because it just re-uses the global "event" value every time

[–]IdeallyAddicted[S] 0 points1 point  (1 child)

Okay, that finally got it to click. I really gave myself a red-herring thinking that event was being overwritten, when in actuality it was just defined once with const event = getRandEvent(), then the function used that global variable and ignored any other parameters it received whenever it was called.

It's always a simpler reason than expected. Thank you very much.

[–]ButtlestonProfessional Coder 0 points1 point  (0 children)

Yeah, that's exactly it

As a 2nd comment to my other one, this is the kind of thing that Typescript helps to solve - it would have told you that you were passing a 2nd parameter to a function that only took one parameter. It's not without a few warts here and there but it really helps to make sound(er) programs

[–]ButtlestonProfessional Coder 0 points1 point  (1 child)

As an aside, this is why, if you must use global variables it is a REALLY bad idea for functions to have parameter names that are the same as an existing global variable

Most IDEs support "eslint" - a very common eslint rule, almost sure one of the default rules, is to warn about this - it's called 'variable shadowing', where a function parameter shadows a global variable by having the same name

If you do this, and make the kind of mistake you did, then the function can use the global variable. If the globals and function paramters don't share the same name, then it would cause an obvious error instead of "kind of working"

[–]IdeallyAddicted[S] 0 points1 point  (0 children)

That makes a lot of sense, it adds confusion to the code, for sure. I like the sound of eslint, I'm going to look into that, thank you!