Why does my code work on this?

Tell us what’s happening:
I passed this challenge but I don’t know why.
I tried to filter non-letter elements from splited array. And my way worked!
filter(word=>word) is this some specific meaning?
I can’t find proper example on google while I’m searching .
Thanks in advance!

Your code so far


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

// Only change code below this line
function urlSlug(title) {
return title.toLowerCase().split(/\W/).filter(word => word).join("-");
}

// Only change code above this line
console.log(urlSlug(globalTitle));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36.

Challenge: Apply Functional Programming to Convert Strings to URL Slugs

Link to the challenge:

it is keeping elements in the array with a truthy value and not keeping those with a falsy value - because filter keeps elements for which the callback return true or a value that evaluate to true, and don’t keep those for which the callback returns false or a value that evaluate to false

and an empty string is a falsy value, any other string is a truthy value

1 Like