Spinal Tap Case conditions

Tell us what’s happening:

Hi! I’m trying to resolve this exercise turning all types of strings into one kind. To achieve this, I make a first if statement where strings with unseparated words receive a replacement to stay true for the second if statement.

In spite of the fact the string matches for the 2nd condition statement, It never goes through. This episode only occurs when the string has no-spaced words. If strings have unseparated word plus another sort of space between them( spaces or underscores) the code works well.

I can’t figure it out where the error is. Thanks for help!

For instance:

spinalCase("AllThe-small Things") // returns all-the-small-things. :white_check_mark:
spinalCase("thisIsSpinalTap") // returns this_Is_Spinal_Tap :x:

Your code so far


function spinalCase(str) {
let spinal = str;

if (/[a-z][A-Z]/.test(str)) {
  spinal = spinal
  .replace(/([a-z])([A-Z])/g,'$1_$2');
} 

if (/_[A-Z]|\s[A-Z]/.test(str)) {
  spinal = spinal
  .split(/_|\s/)
  .join('-')
  .toLowerCase();
} 

return spinal
}

spinalCase('This Is Spinal Tap');

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36.

Challenge: Spinal Tap Case

Link to the challenge:

Your second if is still using the original str instead of spinal. From your description, I assume it should test spinal.

Thanks for asking. I don’t know why does it work with the first example then. If I add an underquote or space to “thisIsSpinalTap”(e.g. “thisIs Spinal_Tap”) the string passes through the two condition statements, turning thisIs in this_Is. It seems that changes are saved in the variable.

Your second if checks whether the original string (str) has an underscore (_) followed by a capital letter or a space followed by a capital letter. “thisIsSpinalTap” does not have either a space or an _, so the second if block is not entered.

1 Like