freeCodeCamp Challenge Guide: Apply Functional Programming to Convert Strings to URL Slugs

Apply Functional Programming to Convert Strings to URL Slugs


Solutions

Solution 1 (Click to Show/Hide)
// the global variable
var globalTitle = "Winter Is Coming";

// Add your code below this line
function urlSlug(title) {
  return title
    .toLowerCase()
    .trim()
    .split(/\s+/)
    .join("-");
}
// Add your code above this line

var winterComing = urlSlug(globalTitle); // Should be "winter-is-coming"
Solution 2 (Click to Show/Hide)
// the global variable
var globalTitle = "Winter Is Coming";

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

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

I am having trouble understanding the second solution at .split(/\s+/). Can someone explain the what happens to the white space?

19 Likes