freeCodeCamp: Bug: Avoid Mutations and Side Effects Using Functional Programming needs to validate the global variable is used in the incrementer function

While reviewing a PR (#39363) that fixes a separate issue on the [Avoid Mutations and Side Effects Using Functional Programming](Avoid Mutations and Side Effects Using Functional Programming) challenge, I noticed the following test does not validate that the value returned from the incrementer function returns a value that is one larger than the fixedValue global variable’s value.

  - text: Your <code>incrementer</code> function should return a value that is one larger than the <code>fixedValue</code> value.
    testString: const newValue = incrementer(); assert(newValue === 5);

A user can simply write the following and pass the challenge without even using the fixedValue variable in the code.

var fixedValue = 4;

function incrementer () {
  // Only change code below this line
   return 5;
  // Only change code above this line
}

I suggest adding a third test, so that we set the value of fixedValue to a different value before calling the function. However, since the user could technically write const fixedValue = 4; instead of var fixedValue = 4;, one way of approaching this would be to use an IIFE: like the following:

  - text: Your <code>incrementer</code> function return a value based on the global `fixedValue` variable value.
    testString: |
      (function() {
       const fixedValue = 10;
       const newValue = incrementer();
       assert(fixedValue === 10 && newValue === 11);
      })()

Before adding a help wanted label, I am looking for feedback from others about this approach to fixing the challenge.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 16 (14 by maintainers)

Most upvoted comments

Ok Thanks 🙂🙂

On Tue, Sep 1, 2020, 20:32 Nicholas Carrigan notifications@github.com wrote:

Heya @Rohit-Arora-0508 https://github.com/Rohit-Arora-0508

Thank you for your interest in contributing! There is currently an active PR that will close this issue, and it looks like that PR is on track for approval. We typically accept the first comprehensive PR that fixes an issue - as such, I would recommend looking for other issues labelled help wanted (or first timers only, if this is your first contribution) and check for ones that do not yet have a linked PR. 🙂

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/freeCodeCamp/freeCodeCamp/issues/39364#issuecomment-684920379, or unsubscribe https://github.com/notifications/unsubscribe-auth/APCTK7AX2LI6NCXRCQORZWLSDUEJVANCNFSM4PTZPOGQ .

@RandellDawson @Sky020 The call to incrementor function inside the IIFE does not refer to the fixedValue variable created inside IIFE. That’s why the assertion will give an error. te

I just want it to be clear to the user why the test is not passing. Is it because they changed the global variable or that they did not use the global variable in the value returned? I could be wrong, but I think adding the 3rd test makes it clearer what has happened to the user.

I would like to open this one up for first timers, since we already have a “fix”. Let’s see if anyone else comments about the suggestion before adding the label though.