Freecodecamp: bug with: The globalTitle variable should not change.

Created on 2 Aug 2020  路  13Comments  路  Source: freeCodeCamp/freeCodeCamp


In the exercise, one of the condition is "The globalTitle variable should not change."
but it returns an error if you modified the var (for debugging) at the top, even if your function doesn't mutate it.
The verification should be that the initial globalTitle variable is the same as the final one, instead of simply looking to match it to a string.

// The global variable
var globalTitle = " Winter Is  Coming";

// Only change code below this line
function urlSlug(title) {
  return title
    .toLowerCase()
    .trim()
    .split(/\s+/)
    .join("-");
};
// Only change code above this line
console.log(globalTitle);
urlSlug(globalTitle);
console.log(globalTitle);

Add a Link to the page with the problem:
Functional Programming: Apply Functional Programming to Convert Strings to URL Slugs

help wanted learn

Most helpful comment

I should not have used the word "mutable". Looking at the challenge again, I think it would be fine to remove the global variable all together as it is not used in any other test (except the first). So, my suggestion is to remove the globalTitle declaration and remove the first test, unless someone else can find a good reason to keep them.

All 13 comments

Hello there,

Ideally, I agree, the tests would be robust enough to account for something like this. However, there is usually a reason for the // Only change code below/above... comments, and users should not expect their code to pass, if they ignore this.

If others agree to change this, I would not be opposed to the first test being removed. I cannot see it being used in any meaningful way in the seed.

I would argue that, as this lesson comes after the Debugging section, users should be somewhat comfortable with using console.log(). Changing a global variable's hard-coded value seems like a less practical debugging method than logging a function call with the argument value to debug.

Additionally, as @Sky020 mentioned, the // Only change code below this line is present to indicate which portions of the seed code should not be modified. 馃檪

for debugging purposes, instead of changing a variable you are told to not change you should change the function call, wiritng there urlSlug(" Winter Is Coming")

but, yeah, it's a thing that trips many up. But dealing with small details is also something that should be learned

I believe the original purpose of the global variable was to actually pass a variable with a known value (globalTitle) to the function and validate that the function does not mutate it. However, the tests do not even check for this case. Such a test would look like:

  - text: The <code>globalTitle</code> variable should not change.
    testString: const dashedTitle = urlSlug(globalTitle); assert(globalTitle === "Winter Is Coming");

As @Ieahleen states, the user should be writing their own calls to the function to test it, instead of changing the global variable. One way to possibly handle this, would be to remove the global variable declaration and use something similar as I mentioned above:
````yml

  • text: The function should not mutate the original value of a variable passed as the argument to the function.
    testString: |
    const globalTitle = "Winter Is Coming";
    const dashedTitle = urlSlug(globalTitle);
    assert(globalTitle === "Winter Is Coming");
    ```
    I would argue, this kind of test should be added to all the functional programming challenges, since this is one of the main concepts to learn.

I can go ahead and submit a PR if we'd like to go this route. 馃檪

@RandellDawson can a string even be mutated in that way? shouldn't strings be immutable and so that is not possible in any case? the test should be to make sure the user reference function parameter instead of a global variable even if the global variable is present, I think

To see if passed in argument is not mutated should be a thing to check with arrays and objects, as those are mutable

@Ieahleen you are correct that strings are immutable, but it is possible to reassign the value of the variable within the function (which is bad practise, thus the reason for the test I believe).

@nhcarrigan I mean if the global variable is removed from the editor

you can't change a global string by operating on function parameter, right?

Correct - if we remove the global variable entirely, then it cannot be modified. If we leave it, it can only be modified by assigning the value to that variable name within the function.

However, in general it is bad practise to modify the values of a function's arguments directly - with strings, it is less harmful than it is with arrays, but I believe that was the intent behind this particular test case.

image

I should not have used the word "mutable". Looking at the challenge again, I think it would be fine to remove the global variable all together as it is not used in any other test (except the first). So, my suggestion is to remove the globalTitle declaration and remove the first test, unless someone else can find a good reason to keep them.

I have some errands to run, but can submit a PR later this evening.

If anyone else would like to tackle this before then, let me know and I'll gladly step back and give you the opportunity! 馃檪

I would like to work on this issue, if that's okay with you @nhcarrigan .

By all means, go ahead! 馃檪

As a note, issues labelled with the "help wanted" tag (like this one) don't require permission before submitting a PR - I only made my earlier comment to ensure my interest didn't deter other contributors.

Feel free to let me know if you have any questions!

Was this page helpful?
0 / 5 - 0 ratings