Hi guys. I’m most of the way there with this. The only thing I can’t figure out how to do is add a dash when there’s no spaces (ie the words are joined up). I’ve been struggling over this for ages and cant find an answer anywhere so just need someone to push me in the right direction please! Many thanks in advance
function spinalCase(str) {
// "It's such a fine line between stupid, and clever."
// --David St. Hubbins
var newStr = "";
for (var i=0; i<str.length; i++) {
if (/[\s_]/.test(str[i])) { //turn space into dash and append to new string
newStr += "-";
} else if (/[A-Z]/) { // turn capital into lowercase and append to new string
newStr += str[i].toLowerCase();
} else { // append rest to new string
newStr += str[i];
}
}
return newStr;
}
spinalCase("thisIsSpinalTap");