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

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

Link to the challenge:

You may have forgot to include the code you are talking about, right now it’s pretty impossible to say anything about it

I was uncertain about how much to I could include while still avoiding spoilers.
Edited the post to include more details.

please post the whole function in both cases. You can use [spoiler][/spoiler] tag to hide them

I should probably have known that. =)
Anyway, sorry about the delayed response. But I added the whole function now.

Check this function call. Look at what becomes after split in one case or the other.

urlSlug(" Winter Is  Coming")

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"?

image

I see a certain difference here. trim will not fix that if you use split(' ') you are going to find yourself with an empty string

New users can only include one embed in each post so I’ll have to split it up. Bare with me. =)

trim() removes leading and trailing whitespace, and is called before split().

Screenshot from 2022-10-01 11-45-24

Result in console from only running trim():
Screenshot from 2022-10-01 11-45-45

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

You are using

Winter is Coming

Try with

Winter is  Coming

notice double space, it’s one of the tests

This is my console when running the above, using trim() before split(" "):

Screenshot from 2022-10-01 11-53-47

Aha!
Wow, I missed that. But yeah, I see what you’re saying now.
Thanks a lot for the help!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.