Hi!
Finally I’ve managed to finish Spinal Tap Case…
Just in time, because it is 3:30 am. Time to bed.
My solution works, but it is far from elegant solution. I can call it “brute force” method.
Please take a look at my code and send me some feedback. If you see some formatting or logical errors don’t hesitate to point it out.
I’m new to programming and I want to learn as much as I can from you guys.
More smart/elegant solution would be appreciated as well.
Thx.
function spinalCase(str) {
var lastOne;
var newArr = [];
var specialTheory = special(str);
function special(input) {
var testRegEx = str.split(/(?=[A-Z])/);
return testRegEx;
}
for(var i = 0; i<specialTheory.length; i++) {
if(i === specialTheory.length - 1 ) {
newArr.push(specialTheory[i]);
} else {
if(specialTheory[i].match(/\s/g)) {
newArr.push(specialTheory[i]);
} else {
newArr.push(specialTheory[i] + ' ');
}
}
}
var newStr = newArr.join('').toLowerCase().replace(/\s/g, '-');
if(newStr.match(/\_-/g)) {
lastOne = newStr.replace(/\_-/g, '-');
} else {
lastOne = newStr;
}
return lastOne;
}
spinalCase("This Is Spinal Tap");//should return "this-is-spinal-tap".
//spinalCase("thisIsSpinalTap");//should return "this-is-spinal-tap".
//spinalCase("The_Andy_Griffith_Show"); //should return "the-andy-griffith-show".
//spinalCase("Teletubbies say Eh-oh");//should return "teletubbies-say-eh-oh".
//spinalCase("AllThe-small Things"); //should return "all-the-small-things".