Functional Programming: Apply Functional Programming to Convert Strings to URL Slugs help

Tell us what’s happening:
i dont understand or know how to approach the solution can someone please briefly explain

Your code so far


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

// Add your code below this line
function urlSlug(title) {
return title
.split(/\W/)
.filter(obj=>{
return obj !=="";
})
.join("-")
.toLowerCase();
}

// Add your code above this line

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 12371.75.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.105 Safari/537.36.

Challenge: Apply Functional Programming to Convert Strings to URL Slugs

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/apply-functional-programming-to-convert-strings-to-url-slugs

can you expand on what you need? what you do not understand?

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 dont understand what the “” after !== stand for.

it’s an empty string, obj !== "" is checking that the character is not an empty string and the filter method removes those.

why there are empty strings? because the split method splits on non word characters with \W
so a string like Hello World (there are three spaces!) becomes ["Hello", "", "", "World"], so then the return method removes the empty strings so now there is just ["Hello", "World"] and the strings can be joined

1 Like