# Spinal Tap Challenge: can someone help with my solution

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.

``````str = `\${str.slice(0, i)} \${str.slice(i)}`; //adding a space
``````

Do a `console.log(str)` just before your return statement and you will see that the string that gets replaced has a dash `-` followed by a space `\s` in the middle.

In order to compensate for this we can change the regular expression.

``````const spaces = /[\s\_]+/
``````

should do it

1 Like

that fixed it, thanks!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.