Hi,
I tried two solutions that were identical except, in the first version I used " " with the split() method. In the second version I tried using regex for one or more whitespace instead: /\s+/.
The results were identical when I tried it in the console. But freecodecamp only passed the one using regex.
1st version using split(" ")
// Only change code below this line
function urlSlug(title) {
return title.trim().toLowerCase().split(" ").join("-");
}
// Only change code above this line
2nd version using split(/\s+/)
// Only change code below this line
function urlSlug(title) {
return title.trim().toLowerCase().split(/\s+/).join("-");
}
// Only change code above this line
I’ve read up on using both but couldn’t find anything on how the two would differ?
Thanks in advance!
Your browser information:
User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:105.0) Gecko/20100101 Firefox/105.0
Challenge: Functional Programming - Apply Functional Programming to Convert Strings to URL Slugs
When testing in the console, it seems to produce the same result to me.
Leading and trailing whitespace is removed with the call to trim(), which comes before split().
So even just returning title.trim() would return “Winter is Coming” if the function is called with " Winter is Coming"?
So when running the code below using split(" “) after trim(). I get the same result in the console for " Winter is Coming” as “Winter is Coming”.
But it’s only using regex with split that I pass the FCC challenge.
// Apply Function Programming to Convert Strings to URL Slugs
// Only change code below this line
function urlSlug(title) {
return title.trim().toLowerCase().split(" ").join("-");
}
// Only change code above this line
console.log(urlSlug("A Mind Needs Books Like A Sword Needs A Whetstone"));
console.log(urlSlug("Winter Is Coming"));
console.log(urlSlug(" Winter Is Coming"));
console.log(urlSlug("Hold The Door"));