Global State Reset Bug
See original GitHub issueAffected page
All JavaScript Challenges
Your code
Something introduced a bug in the runner for JavaScript challenge tests. Now the global variable space for each challenge is separate. This means that invalid solutions that [ab]use the global space are suddenly passing challenges.
For example, see the recursion challenges
// Only change code below this line
let global = [];
function countdown(n){
console.log(n, global);
if (n < 1)
return [];
global.push(n);
countdown(n - 1);
return global;
}
// Only change code above this line
This solution is fragile and misses some key learner misunderstandings about scope, variables, return values, function calls, and the global space. This solution should not pass.
Expected behavior
The test suite should not use a clean/new/reset global variable space for each challenge test executing.
Additional context
We have a canned reply on the forum explaining why global variable [ab]use is bad:
Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.
Example:
var myGlobal = [1];
function returnGlobal(arg) {
myGlobal.push(arg);
return myGlobal;
} // unreliable - array gets longer each time the function is run
function returnLocal(arg) {
var myLocal = [1];
myLocal.push(arg);
return myLocal;
} // reliable - always returns an array of length 2
Issue Analytics
- State:
- Created 2 years ago
- Comments:13 (12 by maintainers)
Top GitHub Comments
if I remember correctly it was somewhere around the issue that is there if the global state stay the same with people using
test
regex method with theg
flag, with tests failing because lastIndex advance and people not getting itI strongly disagree. We should be teaching programming that helps people get job critical skills, not teaching people to write hacky code that happens to pass challenges.
In the specific case of the function above, I see any function that cannot be reused as an inherently wrong solution. A function that looks like it can be called multiple times to achieve its result for a variety of inputs when in fact it cannot is simply bad code.
Also, like I said above, this change removes all of the hard parts of understanding the recursion challenges - working through variable scope, function calls as separate from function declarations, understanding and using return values from functions, etc. I don’t think the recursion challenges ‘should’ be hard ‘just because’. I think recursion is inherently hard but these challenges are an opportunity to reinforce critical understanding that professional developers need. Giving the learners a way to bypass this learning does not help them.
By introducing this change, we fundamentally weaken the level of understanding required to pass the challenges in this section and we allow far lower quality solutions to pass the projects for this section - in effect, this change downgrades what the JavaScript certificate means.