Apply Functional Programming to Convert Strings to URL Slugs / Global variable should not change - bug?

Potential bug maybe? Code works great but still an error “the global variable should not change”. From my tests, it does NOT change… Anyone knows what’s happening?
Thanks in advance!!!

Your code so far


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

// Add your code below this line
function urlSlug(title) {
    let arr = title.split(/\s/);
    let filteredArr = arr.filter((elem) => elem !=="");
    let str = filteredArr.join("-");
    str = str.toLowerCase();
    return str;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 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

Probably a bug because you aren’t reassigning the global variable, and string is immutable.

Aside from it, you can use str.match(/\w+/g) to eliminate the use of filter. Also, you might want to make this function one liner because it is idiomatic to do so in this case; you don’t really need to initialize variable on every intermediary step.

2 Likes

Thanks a lot for the very helpful advice! Will try to integrate that in my future coding…

True - an accidental " " added itself to the title… thanks for the answer!