Apply Functional Programming to Convert Strings to URL Slugs - global variable

Hello fellow campers!

Please help me with the following challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/apply-functional-programming-to-convert-strings-to-url-slugs

I’m supposed to change the string to url slugs. The .split(), .join(), and .toLowerCase() functions were the straightforward solutions, but I couldn’t figure out why I was getting the issues with the globalTitle variable located outside of the function.

let globalTitle = "  WinTer Is     *!&^@#*@(*$ Coming!";

// Add your code below this line
function urlSlug(title) {
  return title.split(/\W/)
              .filter((e) => e !== '')
              .join('-')
              .toLowerCase();              
}
// Add your code above this line

let winterComing = console.log(urlSlug(globalTitle)); // Should be "winter-is-coming"
console.log(globalTitle);

The code above prints the right results, but i kept getting the following message:

The globalTitle variable should not change.

When i try to print the globalTitle variable after the function call, i still see the same original string… unchanged.

Any word of advice on this issue is greatly appreciated :upside_down_face: !

Hi @joechef,

This kind of problem can be really frustrating. Especially when you know that your code is right.

Based on a first look, I think the problem here may be more to do with your console.log than your solution. Maybe take another look at which variable you’re logging. Also, maybe you could try getting rid of the console.log, and running the tests to see what happens. That might help you along the path.

1 Like

Hello @jrsinclair,

Thanks for the reply!
I did try your recommendation, and it worked just fine haha… I thought I tried the function call with the original code already, but i guess i missed that one. Still, I assume that using console.log in the actual program would not affect the scope of the variables that I use… would that be correct?

You should reset this as the original "Winter Is Coming" to pass the tests, because tests are checking that you didn’t change the original string, and the only way is to check if globalTitle has still that value

1 Like

also, know that you can’t use console.log() like that because it returns undefined so you have actually written let winterComing = undefined;
this is important to know in case you need to return something inside a function, you can’t use console.log() in the return statement

2 Likes

Hello @ilenia,

Thanks for the reply and the reminder about the return statement. It’s just like how i needed a couple reminders to declare the i iterator in the for loop with let. The details in programming are so crucial lol

You’re right. Using console.log shouldn’t modify any variables. I think what’s happening here is that they’ve put a check in to make sure that you don’t modify the globalTitle variable. It’s not actually checking if globalTitle is changed. Instead it’s checking whether you typed it at all (since you shouldn’t need to).

Edit: Actually, @ilenia’s answer makes more sense :slight_smile: