Convert string to URL slugs. Help with passing one of the tests

Tell us what’s happening:

Can somebody help me? My solution doesn’t pass one of the tests (urlSlug(" Winter Is Coming") should return "winter-is-coming" .) and I don’t know why.

I tried priting to the console the value I’m returning and it looks like it is returning what it should return indeed. Does somebody know what might be causing this? Thanks!

Your code so far

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

// Only change code below this line
function urlSlug(title) {
title = title.trim()
let lowercaseTitle = title.toLowerCase();
let splitTitle = lowercaseTitle.split(/\W/)
console.log (splitTitle.join("-"))

// Only change code above this line

urlSlug(" Winter Is Coming")

Your browser information:

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

Challenge: Apply Functional Programming to Convert Strings to URL Slugs

Link to the challenge:

urlSlug(" Winter Is Coming") returns:

It produces an extra -.

1 Like

There are two ways I can think of to solve this problem.
First, is by using the combination of the following:

  • trim() to remove white spaces at start and end
  • split(’ ') to split words per space
  • join(’-’) to join them
  • toLowerCase() to make all strings lowercase.

The second option I can think of is through the use of the following:

  • trim(),
  • toLowerCase(), and
  • replace(/\s/g,’-’) to replace white spaces between words with ‘-’

Also, do not forget to ‘return’ the result of your function instead of wrapping it inside console.log()

I hope it helps. :slight_smile:

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Thank you for understanding.