Can anyone suggest whats wrong in the code? I

I don’t get what’s wrong in my answer. Its working fine thou for the mentioned params.
Your code so far


function urlSlug(title) {

const result = title.trim().split(/\W/).join('-').toLowerCase();

return result;

}

// Only change code above this line

console.log(urlSlug(" Winter is Coming "))

Do you not understand how it is failing or do you not understand why it is failing?

To understand the problem, consider this:

function urlSlug(title) {
  const result = title.trim().split(/\W/).join('-').toLowerCase();
  return result;
}

console.log(urlSlug(" Winter Is  Coming"))
// winter-is--coming

So, how do you fix that? What is going wrong?

If you can’t see it, I would recommend breaking up your code a little so you can see what is happening.

function urlSlug(title) {
    const splitValues = title.trim().split(/\W/)
    console.log('splitValues', splitValues)

    const result = splitValues.join('-').toLowerCase();
    return result;
}

I think this could be fixed with an adjustment to your regex.

1 Like

Thanks a load, Kevin. I really appreciate you taking your time and explaining this out. I really must admit developer’s community is the best out there.

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 (’).

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