Apply Functional Programming to Convert Strings to URL Slugs (Winter is here!)

Tell us what’s happening:
Console.log and return “winter-is-coming” as requested. Still, not successful.

All points passed except: " urlSlug(" Winter Is Coming") should return "winter-is-coming" ."

Anyone please explain why it is not going through???

cheers!

Your code so far


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

// Add your code below this line
function urlSlug(title) {
var titleToSlug = title.split(/\s+/g).join("-");
return titleToSlug.toLowerCase();
}
// Add your code above this line

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

console.log(winterComing); //winter-is-coming

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36.

Challenge: Apply Functional Programming to Convert Strings to URL Slugs

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

1 Like

Problematic is case starting with empty space. Your function adds hyphen also at the beginning there.
Take a look:
console.log(urlSlug(" Winter Is Coming"));

1 Like

You have to really pay attention to how your split will behave in edge cases such as there’s spaces at begin and at the end of the input string, or when your string is separated by things other than spaces . For trailing spaces, you can use trim method to deal with that. I recommend using regex101 website to test how your regex will behave in these many scenarios , it really helps clarifying.

1 Like

Oh yes,

var globalTitle = " Winter Is Coming";

// Output: -winter-is-coming

overlooked that part! wow, you’re very responsive.
Thanks for your time.

Great, thanks for recommending regex101 this will be very helpful since I always have problem with regex. But, how will trim() help tho? Is trim a higher order function? I have not come across it before.

cheers~

trim is a method that trim spaces from beginner and end
google it for more infos!

also, don’t change globalTitle, if you want to test different strings you need to change the string in the function call
console.log(urlSlug(" Winter Is Coming ")
if you change globalTitle tests fail

1 Like

Hey there! Adding to ieahleen’s response, you can find out more about methods like this looking at the JS documentation. Personally, I like devdocs dot io for that. It has documentation for other programming languages too.

1 Like

Thanks for the heads up on changing the variable globalTitle. Will look into Trim()

Cheers!

Wow, just checked out devsdoc dot io. This site is a gem! Thanks again for recommending all these helpful sites!

Cheers!