3 ms·
I think it would be 0, [] a and b are shadowed in the foo function so the function only acts on the shadowed (and copied) variables. Once you return from foo,
by ornornor 5y ago
I think it would be 0, []
a and b are shadowed in the foo function so the function only acts on the shadowed (and copied) variables. Once you return from foo, the const a and b are unchanged.
Not a great question as a pass or fail test imho because if you use shadowed names often enough to be able to parse this code in your head, you’re writing bad code. There is a reason why shadowing names is a bad practice: it’s hard to figure out what the end result is and account for side effects! But as a discussion on all the points above then it would teach the recruiter something about what the candidate knows rather than using this as a trivia question.
- dang 5y agoThe const on b only applies to the object reference to b, and doesn't prevent foo from changing the contents of b. No doubt that's why the test question includes an array in the first place. At least I assume that's how const works; I've not used const in JS.
- ornornor 5y agoThat’s possible. In reality, if I ever came across that piece of code and had a bug to fix in it, the first thing I’d do is rename a and b in one scope or the other because it’s just asking for trouble otherwise and it’s not easy to understand what happens to a and b. That’s why I think using this question as a trivia question is bad. But the discussion we’re having about it clearly shows we both know what to look out for, that we’re competent, and is way more interesting than running JS code in our heads. Even if either of us gets the answer wrong, it doesn’t really matter as much as how we got there in my opinion.