Spinal Tap Case

Spinal Tap Case
0

#1

I’ve been trying to make a solution to Spinal Tap Case without using regex, because I think I’m scared of regex. And because I love to avoid something I think is hard by doing something that’s actually harder. ANYWAY, of course I could go ahead and solve it using regex, but now I want to know why my solution, which I think ought to work, isn’t working.

function spinalCase(str) {
  var splitString = str.split("");
  var resultString = "";
  resultString = resultString.concat(splitString[0].toLowerCase()); //Take care of the first character right off the bat
  
  for (var i=1; i<splitString.length; i++) {
    var char = splitString[i];
    if (char === char.toUpperCase() && splitString[i-1] !== (" "||"_"||"-")) { //If the character is upper case, and the previous character is not whitespace...
      resultString = resultString.concat("-", char.toLowerCase());
    } else if (char === (" "||"_"||"-")) {
      resultString = resultString.concat("-");
    } else {
      resultString = resultString.concat(char.toLowerCase());
    }
    console.log(resultString);
  }
  
  return resultString;
}

The output converts spaces to hyphens, but still inserts a space after the hyphen. I.e.:
splitString = ["T", "h", "i", "s", " ", "I", "s", " ", "S", "p", "i", "n", "a", "l", " ", "T", "a", "p"]
I inserted a console.log(resultString) line at the end of the for loop, and get:

"th"
"thi"
"this"
"this- "
"this- i"

… etc. The space from the original string clearly turns into a hyphen, so where’s that other space coming from?