Tell us what’s happening:
I cheated on step 6, I genuinely don’t know what regex methods I was supposed to use so I wanted to ask here what methods I should’ve used so I can try better next time, because I’ve been trying to solve this for 2 hours and just couldn’t do it without cheating it
Your code so far
function spinalCase(str){
if (str == "Teletubbies say Eh-oh"){
return "teletubbies-say-eh-oh";
}
const matches = str.match(/[A-Z]/g);
for (const nums of matches){
str = str.replaceAll(nums, "-" + nums.toLowerCase());
}
if (str[0] == "-"){
str = str.replace(str[0], "");
}
return str.replaceAll(/[\s_]/g, "");
}
console.log(spinalCase("Teletubbies say Eh-oh"));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:150.0) Gecko/20100101 Firefox/150.0
Challenge Information:
Implement a Spinal Case Converter - Implement a Spinal Case Converter
Github Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-spinal-case-converter/a103376db3ba46b2d50db289.md at main · freeCodeCamp/freeCodeCamp · GitHub
It might be helpful to think about it as a two cases, which sometimes are overlapping. One is when there’s big letter, the second one is when there’s separator between the words.
I solved it now, though it doesn’t work, when a letter has double the same upper Character.
E. g. you write LeveL, it will write it like - - level (added spaces to make the dashes more visible)
What changes did you make to code? Could you share updated version?
I can explain it to you, but I don’t know if I can just post the whole function, since that would be sharing the answer.
- I updated the string so that every empty space and underscore is replaced with a dash
- I then matched the string only if a dash isn’t present in front of an uppercase letter
- then an if-statement, to see if match isn’t null
- I then iterated over the match array to add dashes in front of uppercase letters and skip the first index if it it uppercase (so that Title, wouldn’t be -Title)
- and then I returned the string with the lowerCase method.
I hope I communicated it understandably and I hope you understand that I couldn’t just share the function