JS Apply Functional Programming to Convert Strings to URL Slugs

Hello everyone!

I am having some trouble passing this challenge in the JS module.
I understand that my problem is somewhere with the spaces but I have tried sorting it out with a regular expression and it ruins the rest.

Anyone has any suggestions on how to fix it , without being the recommended solutions because one envolves “trim” which I haven’t learned yet, and the other filter which I have not mastered and am still confused about, or is filter the only way to go here?

Exercise:
Fill in the urlSlug function so it converts a string title and returns the hyphenated version for the URL. You can use any of the methods covered in this section, and don’t use replace . Here are the requirements:
The input is a string with spaces and title-cased words
The output is a string with the spaces between words replaced by a hyphen ( - )
The output should be all lower-cased letters
The output should not have any spaces

// Only change code below this line

function urlSlug(title) {
var a=title.split(" ");
var b=a.join("-");
var c=b.toLowerCase();
return c
}
// Only change code above this line

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

gives back= -winter-is–coming

what have you tried here? a regex can be what solve the issue, but what have you tried?


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

1 Like

I just tested it again and it worked!
I was still trying to use split even after using the Regular expression hence the previous error.
Now it works!!
(I feel really proud right now)

// Only change code below this line
function urlSlug(title) {
let regex=/\S+/g;
let result= title.match(regex);

let b=result.join("-");
let c=b.toLowerCase();

return c


}
// Only change code above this line
console.log(urlSlug(" Winter Is  Coming"))

Thank your for the info on the readability! and for making me go back to a failed attempt 1.5 hours ago that was actually on the right track :smiley:

I mean you can use a regex as argument of split, so thay would not have been wrong to do

good job in solving it!

1 Like

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