Questions on Functional Programming: Apply Functional Programming to Convert Strings to URL Slugs

Hello,
Can someone please help me out of my current state of stuck-ness? I believe that my problem is in not being able to specify exactly how I want to split the string into my variable array with whitespace in mind. That is, how can I target the whitespace at the beginning, middle, and end of a given string so that when I use split() on the string, the resulting array “ignores” ALL whitespaces? Apologize if I’m unclear/unnecessarily verbose/etc.


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

// Add your code below this line
function urlSlug(title) {
    let lowCase = title.toLowerCase();
    let newArr = lowCase.split(/\s+/);
    
   console.log(newArr.join("-"));
    return newArr.join("-");
   
  
}
// Add your code above this line

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

You’re splitting on whitespace. What do the failing tests say?

Hello!
Here’s the message that I’m receiving:
// running tests
urlSlug(" Winter Is Coming") should return “winter-is-coming”.
// tests completed

If I “console.log” it, I get a returned string that contains a hyphen at the beginning: “-winter-is-coming” instead of"winter-is-coming’

You can remove whitespace at the beginning and end of a string using either replace() or trim().

1 Like

Alright–I’ll try! Thanks :slight_smile: