Apply Functional Programming to Convert Strings to URL Slugs I'm stuck

I pass all the test except for one, when there are two spaces in a global variable like that:

" Winter Is Coming"

so my output is like that “winter-is–coming” instead of this “winter-is-coming”

I made the loop to iterate through and delete the additional space using splice() method, but it doesnt work and i dont know why, can anybody help?

Your code so far


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

// Add your code below this line
function urlSlug(title) {
  var sorted;
  var joined;
  var reduced;
  sorted = title.split(" ");
  if (title[0] === " ") {
   sorted.shift();
  }

  for (var i = 0; i < sorted.length; i++) {
    if (sorted[i] === " " && sorted[i + 1] === " ") {
      sorted.splice(sorted[i], 1);      
    }
  }
console.log(sorted);
    

  joined = sorted.join("-").toLowerCase();
console.log(joined);
  return joined;
  
  
  
  
  
}
// Add your code above this line

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36.

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

use a regular expression enclosed between ^ and $.
You might want to review that altogether.
Then you can generate an array of all the words you need without having to worry about that extra space.
I won’t touch on other regex as that would be doing the solution for you.