question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Global State Reset Bug

See original GitHub issue

Affected 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

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/use-recursion-to-create-a-countdown

// 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:closed
  • Created 2 years ago
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
ieahleencommented, Sep 22, 2021

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 the g flag, with tests failing because lastIndex advance and people not getting it

1reaction
jeremyltcommented, Sep 21, 2021

I 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

useGlobalState resetting when I navigate - Stack Overflow
So I am using a Global state to store whether a user is logged in or not and his image. I use the...
Read more >
Reset Global State - Support - Syncthing Community Forum
Hey there! macOS stores the folder icon is some weird hidden file inside the folder called Icon\r That is, the filename is Icon...
Read more >
Block cracking state not reset upon rejoining a world - Mojang
The block cracks are still there, and the block's state is not restored even if you rejoin the world. See attachments below for...
Read more >
What's the state about the VFIO reset bug? - Reddit
I use a Vega 64 and after installing the newest drivers in the Windows VM the bug is fixed. Quite a hassle to...
Read more >
Reset Domain Crossing: 4 Fundamentals to Eliminate RDC ...
Glitches for asynchronous resets. A design can have multiple sources toggling at different times resulting in a glitch at an asynchronous reset.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found