Unable to get result

Hi I’m not sure how to remove the extra space in the beginning of the strings. I noticed in the solution “hint” they used .filter(obj => { return obj !== ""; }) but I’m not understanding how and how to solve with my approach. Thanks

**Your code so far**

// Only change code below this line
function urlSlug(str) {
return str
    .toLowerCase()
    .split(" ")
    .join("-"); 
}
console.log(urlSlug(" Winter Is Coming"));
// Only change code above this line
**Your browser information:**

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

Challenge: Apply Functional Programming to Convert Strings to URL Slugs

Link to the challenge:

Hi,

String.prototype.trim() method removes whitespace from both ends of a string.

1 Like

I wouldn’t use .filter().

When I Google “strip space strings JavaScript”, I get a bunch of results, to include:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim

When I add this into your code, I get that checkmark :slight_smile:

1 Like

Thank you @sitek94 @JeremyLT but for some reason it’s not passing one test, even though it is returning the right string “winter-is-coming”

// Only change code below this line
function urlSlug(str) {
    return str
        .trim()
        .toLowerCase()
        .split(" ")
        .join("-");
}
console.log(urlSlug(" Winter Is Coming"));
// Only change code above this line

Ah, that isn’t a trimming at the beginning issue. That is a trimming double spaces issue.

Not sure what that means

urlSlug(" Winter Is  Coming")
urlSlug(" Winter Is__Coming")

It is hard to see, but there are two spaces in there.
Your split should handle multiple spaces.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.