Hello
Sorry it won’t let me link or reply to the challenge (freeCodeCamp Challenge Guide: Spinal Tap Case).
I have come up with the following solution, which uses a for loop to test for a capital and a space. If there is a capital and a space then it will use slice to insert a space into the relevant part of the string.
function spinalCase(str) {
const spaces = /\s+|_+/g;
const capitals = /[A-Z]/g;
for (let i = 1; i < str.length; i++) {
if (capitals.test(str[i]) && !spaces.test(str[i - 1])) {
str = `${str.slice(0, i)} ${str.slice(i)}`; //adding a space
}
}
return str.replace(spaces, '-').toLowerCase();
}
As you can see it works for most the test cases except for ‘The_Andy_Griffith_Show’
I think it is something to do with whether I am using the regex search globally or not but after a lot of testing I can’t figure out why, if you can see my problem I would love it if you could let me know what I have done wrong.
I have gone through the other solutions in the topic and understand them (and I understand why they are better than mine), but I would still like to make my solution work.