Apply Functional Programming to Convert Strings to URL Slugs - original array mutated error

Hi,

Could you help me why my script does not pass?
I get the following error:

“The globalTitle variable should not change.”

I create a copy of the passed string with slice and using methods on that.

If I check and use console.log() then the original array not mutated.

Thanks

Your code so far


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

// Add your code below this line
function urlSlug(title) {

  return title.slice(0).trim().split(/\s+/g).join('-').toLowerCase();
}
// Add your code above this line

var winterComing = urlSlug(globalTitle); // Should be "winter-is-coming"

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/67.0.3396.99 Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/apply-functional-programming-to-convert-strings-to-url-slugs

You are getting that message, because you accidentally change the global variable from:

var globalTitle = "Winter Is Coming";

to

var globalTitle = " Winter  Is Coming";

Remove that extra space at the beginning of the string and try running the tests again.

1 Like

Thank you,

I would not have found out the cause of this error.

Thanks, I had this same problem. Seems a little silly that you can’t test out the other inputs without having to remember to change it back to the original.

To test out other inputs, you could simply change the following line. You would put whatever string you want to test instead of globalTitle.

var winterComing = urlSlug(globalTitle);