Apply Functional Programming to Convert Strings to URL Slugs Is this a correct solution?

Tell us what’s happening:

So, I solved this and it passed the test but when I was looking at other people’s solutions, I found them that they used “.trim()” instead of “filter(boolean)” to filter the spaces. Is there any difference between both?

Your code so far

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

// Add your code below this line
function urlSlug(title) {
  return title.toLowerCase().split(/\W+/g).filter(Boolean).join("-");

// Add your code above this line

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0.

Link to the challenge:

1 Like

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

I think the trim version would be more readable, because you have to stop and think a bit and realize the filter using Boolean as the callback function is just removing the spaces. This line would be a good candidate for a comment to explain why you used the filter in the way you did.

Again, there is nothing wrong with the solution as you were thinking outside the box, but trim is a little more straight forward when it comes to readability in my opinion.

1 Like

I guess you’re right, .trim() looks more readable than filter(boolean) the way I did it but I think that as long as they both work, there are no problem(as long as I add comments to explain why I did that)

Thank you for the useful explaination!

1 Like