function urlSlug(title) {// Only change code below this line
var splitted=title.toLowerCase().split(/\s/);
for(let i=0; i<splitted.length; i++){
if(splitted[i]===""){
let arr=[...splitted];
arr.splice(arr.indexOf(""), 1);
return arr.join("-");
}else{
return splitted.join("-");
}
}
}
// Only change code above this line
console.log(urlSlug(" Winter Is Coming"))
**Your browser information:**
User Agent is: Mozilla/5.0 (Linux; U; Android 7.0; TECNO P701 Build/NRD90M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/88.0.4324.181 Mobile Safari/537.36 OPR/24.0.2254.115784.
Challenge: Apply Functional Programming to Convert Strings to URL Slugs
So, the problem is the input " Winter Is Coming" (note the extra space in between “is” and “coming”) is returning winter-is--coming.
So, the problem is that
var splitted = title.toLowerCase().split(/\s/);
// ["", "winter", "is", "", "coming"]
Notice the 4th element. So, split is seeing that extra space and is creating an empty string element. There are a couple of ways to fix this. The most elegant would be to fix it at the source by adjusting the regex. In regex there are quantifiers that tell it how many of “whatever” to match. By default, this is matching one character that fits \d. There is a quantifier that will tell it to match “one or more”. With that, I can fix your code by adding one character.
Can you find it? Let us know if you need more of a hint.