Help me to Convert Strings to URL Slugs in javascript

Tell us what’s happening:

Hello everyone!
I wrote this solution to end the challenge, the problem is that it creates a dash also on the last word and, when it finds a phrase with a space, it inserts two dashes instead of one. Can you explain and make me understand how to solve and where am I wrong?

Thank you

Your code so far


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

// Add your code below this line
function urlSlug(title) {
let newStr;
newStr = title.toLowerCase()
.trim()
.split(' ')
.map((el, index) => el + '-')
.join('');
console.log(newStr);

}
// Add your code above this line

var winterComing = urlSlug(globalTitle); // Should be "winter-is-coming"

Challenge: Apply Functional Programming to Convert Strings to URL Slugs

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/apply-functional-programming-to-convert-strings-to-url-slugs

wrong

//... .split().map().join()

try

//... .split(' ').join('-')
1 Like

Thanks so much!
Sometimes I get lost in a glass of water! :sweat_smile: