Using split method correctly?

I’m so close to passing this challenge but I’m getting an extra hyphen? I’m getting this
-winter-is-coming

Here’s my code

// Only change code below this line

function urlSlug(title) {

return title.split(" ").join("-",).toLowerCase();

}

// Only change code above this line

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

Look closely at some of the inputs the tests are using to test your function:

urlSlug(" Winter Is Coming")

Do you notice anything special about this string, say compared to this one:

urlSlug("Winter Is Coming")

I wonder if there might be a JS method that could help you trim the unnecessary white space on the first one?

1 Like

I’m guessing by using some sort of regex in the split method to remove the white space?

I suppose you could do that but the key here is that you want to get rid of any unnecessary white space at the beginning and end of the string. There is actually a JS string method for doing this exact thing.

1 Like

I just googled it. There is a trim method to remove the white spaces in the front and the end of the string…

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

My new code now is return title.trim().split(' ').join("-").toLowerCase() and it prints winter-is-coming… but it’s not passing the test?

When I test your code, I see this result in the console.

winter-is--coming

You are not passing the test case that has an extra space between the words Is and Coming.

That’s why you have two -- when there should only be one.

You need to modify your code to account for that.

1 Like

Hi @talk2chibu !
Welcome to the forum!

We don’t encourage users to give out full solutions when assisting others on the forum.
It is best to help them through hints so they can solve it themselves.

But also, the directions say not to use the replace method so your code would not pass the FCC tests.

1 Like

I get your point, sorry I did that, I’ll be conscious of it next time