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"))
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.
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