Converting string to URL slugs problem(1)

Tell us what’s happening:
hello, I’m trying to pass the challenge and only one pass is refusing to go through. But I see no problem in the solution.

  **Your code so far**

// Only change code below this line
function urlSlug(title) {
return title
.toLowerCase()
.split(/\W/i)
.join("-");

};
// Only change code above this 
console.log(urlSlug("Winter Is Coming"));
console.log(urlSlug("A Mind Needs Books Like A Sword Needs A Whetstone"));
console.log(urlSlug("Hold The Door"));
console.log(urlSlug(" Winter Is  Coming"));
  **Your browser information:**

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

Challenge: Apply Functional Programming to Convert Strings to URL Slugs

Link to the challenge:

What happens when you have multiple spaces in a row? I think your solution refuses to correctly handle this case.

use the output “Run the Tests” gives you. In your case it doesnt pass for urlSlug(" Winter Is Coming") . Use your solution and print out what your function returns. Its -winter-is-coming. You have an additional hyphen in front of the text. If you check what each of the methods you use in your function returns, you will notice how your split() method handles white space in the beginning of a sentence. It returns an additional empty string, in the array, which you then proceed to concatenate with a hyphen. In order to fix that, you should think of a way to properly handle white spaces in the beginning or an end of a string.

Ah, its both. This solution does not handle leading/trailing whitespace or repeated whitespace characters:

console.log(urlSlug(" Winter Is  Coming"));

produces

"-winter-is--coming"

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