I'm attempting to insert "-" before capital letters in a string. For example, transforming helloWorld into hello-world.
Despite my efforts, the dashes are being placed incorrectly within the output array. For instance, changing thisIsSpinalTap to this-I-sSpin-alTap
What could be causing this issue with my code?
function spinalCase(str){
str = str.replace(/ /g,'-');
strArr = str.split("");
for(i=1;i<str.length;i++){
if(str.charAt(i)==str.charAt(i).toUpperCase()&&str.charAt(i)!=="-"){
console.log(i);
strArr.splice(i,0,"-");
}
}
return strArr.join("");
}
spinalCase('thisIsSpinalTap'); // should give this-I-sSpin-alTap