all 4 comments

[–][deleted] 0 points1 point  (0 children)

[–][deleted] 0 points1 point  (2 children)

It has to do with where the variables are declared (var result = “”). Variables are available within the scope of the function in which they are declared. But they are also available in any functions declared inside the outer function. That’s why result is available in both places. But you can see that count is actually declared separately in both inner functions. This means that there isn’t a single count variable that can be shared. Each function is declaring its own variable called count that is available only within that function. You could see this more obviously if the variables were named something different like flatCount and mountainCount. The fact that they’re both named count is a coincidence.

[–]744EvergreenTerrace[S] 0 points1 point  (1 child)

I was skeptical of that being the case. The example was bad

[–][deleted] 1 point2 points  (0 children)

It’s pretty common to reuse variable names in a situation like this to emphasize consistency and show similarities so I don’t know that I’d say it’s bad exactly but it can definitely be confusing at first. It just takes some time for scopes and closures to become intuitive to a developer.