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
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"));
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.
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.
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
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.