freeCodeCamp Challenge Guide: Spinal Tap Case

@ArielLeslie @jemagee I am also not good with regular expressions, so I came up with this.

function spinalCase(str) {

  var result="";
  for (var i = 0; i<str.length; i++){
    
    var codeCheck = str.charCodeAt(i);
    
    if(codeCheck>=65 && codeCheck<=90){
      result+=String.fromCharCode(codeCheck+32); // converting to lower case
    }
    else if(codeCheck>=97 && codeCheck<=122){
      
      if(str.charCodeAt(i+1)>=65 && str.charCodeAt(i+1)<=90){ // if the next character is Uppercase, then add the "-"
        result+=String.fromCharCode(codeCheck);
        result+="-";
      }
      else
        result+=String.fromCharCode(codeCheck);
      } 
    else
      result+="-";
  }
  return result;
}

spinalCase('thisIsSpinalTap aBCDefK');

It seems to be working fine.
If you detect any flaws in this code, kindly reply. I need to get better at regular expressions. For now, just trying to avoid the use of it.

3 Likes

It can be done in a single line with:

return str.replace(/([a-z])([A-Z])|[_|\s]+/g, ‘$1-$2’).toLowerCase();
</>

54 Likes

@thegreaterpanda

Could you help me understand your solution a little bit more? I like the conciseness of it, but I don’t understand the last part.

So far, this is my understanding of that code:

  • Return the result of the replace method called on the string argument (thereby creating a new “str”),
  • For every lower case a to z letter (RegExp), every capital A-Z letter (RegExp) -OR- underscore or blank space characters of at least one or more, global search…
  • …and then the syntax for the $1 and $2 I don’t understand.

The MDN documentation says that $n takes any positive integer below 100 and assigns it to the nth “parenthesized submatch string”, assuming that the first argument fed to .replace is a RegExp object.

So, with the above syntax, I am guessing it would read: for every capital or lower case a-z OR a blank space/underscore, replace it with… that same letter or blank space? I’m really not sure. :confused:

11 Likes

This is BUGGY -

with this string

spinalCase('this_Is-SpinalCase iI')

it returns:

this-is--spinal-case-i-i

which should NOT pass the 4th requirement; the return has an extra ’ - ’ in it …

4 Likes

Sure thing! You’re right about the first part. So the regex stores the match found in each bracket as a ‘$’ variable. It looks for a small letter next to a capital letter. Once it has found that it assigns the first letter to the ‘$1’ variable and the second letter to the ‘$2’ variable. It then replaces that match with the string on the right which contains a dash in the middle between the two characters and calls toLowerCase on the result.

9 Likes

I found the same thing testing it with this string: ‘AllThe-Small ThingsFor’. The combination of the hyphen and uppercase letter trips up the advanced solution but not the others. However, with your string none of the solutions work. I’m assuming you want the result to be ‘this-is-spinal-case-ii’, right? I guess this is an edge case as you would have to tell the algorithm that ‘II’ is not a word that you can split up but a roman number.

To make the solution work for my example string I added a replace to it:

function spinalCase(str) { 

    return str.split(/\s|_|(?=[A-Z])/).join('-').replace(/--/g, '-').toLowerCase();
	
}

It is not very elegant so any suggestions for improvement would be appreciated.

3 Likes

adding non capturing group like:
return str.split(/\s|_|(?=[A-Z])|(?:-)/).join('-').toLowerCase()
will get rid of - all together, since when joining we add them anyway, those fixing the issue.

As for “iI” being separated, well the idea is to catch entire words beginning with capital letter. Identifying what is basically a typing error is slightly beyond scope of this exercise I recon. Just imagine writing regex to extract the word from random mix of low and upper caps like FrEEcoDeCaMp :slight_smile:

4 Likes

I’m not good enough with regex,but this is my approach.

function spinalCase(str) {
  // "It's such a fine line between stupid, and clever."
  // --David St. Hubbins
  var firstLet = (str.split(''))[0];
  str = str.slice(1,str.length);
  str = str.replace(/\s/g,'-');
  str = str.replace(/([A-Z])/g,'-$1');
  str = str.replace(/_/g,'-');
  str = str.replace(/-+/g,'-');
  return (firstLet + str).toLowerCase();
}

spinalCase('thisIsSpinalTap');

That wasn’t easy. Then I found a solution which makes me happy :grinning:

function spinalCase(str) {
  return str.replace(/(\w)[ _]?([A-Z])| /g, "$1-$2").toLowerCase()
}
2 Likes

My code:

function spinalCase(str) {
  
  //fist deal with the Camel case strings
  str = str.split(/(?=[A-Z])/).join(" ");
  //create patterns that will be replaced
  var patterns = [/_+/g, /\s+/g,/-+/g];
  
  for(var i=0; i<patterns.length; i++){
    if(patterns[i].test(str)){
      str = str.replace(patterns[i], '-');
    }
  }
  
  return str.toLowerCase();
}

//test
spinalCase('AllThe-small Things');
3 Likes

My solution:

function spinalCase(str) {
  const regEx  = /([a-z])([A-Z])/g;
  const regEx1 = /\s+|_+/g;
  const subst  = '-';
  
  const result = str.replace(regEx, '$1-$2')
                    .replace(regEx1, '-')
                    .toLowerCase();
  
  return result
}

spinalCase('This Is Spinal Tap');
4 Likes

Here’s the solution that worked for me:

function spinalCase(str) {
  
  regExp = /[\W_,.]/g;
  
  return str.replace(regExp, '-')
                .replace(/([a-z\d])([A-Z])/g, '$1-$2')
                .toLowerCase();   
}
1 Like

I got some trouble with this challenge, but finally I managed to solve it. My major problem was the “upper case letter condition” to insert a “-”. I know there are short and easy ways to solve this using regExp but it’s a bit complicated for me since I don’t have so much practice in that (by now), so I wanted to find my way through this challenge.

function spinalCase(str) {
  // replace spaces [/s] and underscore [_]
  return str.replace(/[\s_]/g, "-")
            // replace lowercase letter [a-z] followed by uppercase letter [A-Z]
            .replace(/[a-z](?=[A-Z])/g, function(match) {
              match = match + "-";
              return match;
            })
            .toLowerCase();
}

It’s not elegant code but…

function spinalCase(str) {

  str = str.replace(/ /g, "-");
  str = str.replace(/_/g, "-");
  str = str.replace(/[A-Z]/g,replacer);

  function replacer(match, offset, string) {
    return (offset ? '-' : '') + match.toLowerCase();
  }

  str = str.replace(/--/g, "-");
  
  return str;
}
spinalCase("This Is Spinal Tap");
1 Like
function spinalCase(str) {
  // "It's such a fine line between stupid, and clever."
  // --David St. Hubbins
  var arr = str.split("");
  var regEx = /[A-Z]/g;
  for(var i =0; i<arr.length;i++){
    if(regEx.test(arr[i])){
      if (i!=0){
      arr.splice(i, 0, " ");
      }
    }   
  }      
  var filtered = arr.join("").toLowerCase().replace(/_/g," ").replace(/\s+/g," ").replace(/[^A-Za-z0-9]/g,"-");      
  return filtered;
  //finito
}

I really need to get better with regular expressions. Some algorithms here have just three to four lines of code!! I’ll get there soon though. I’ll get there. :slightly_smiling_face:

Hello !

Here’s my one liner solution and very proud of it:

    function spinalCase(str) {
      // "It's such a fine line between stupid, and clever."
      // --David St. Hubbins   
      return str.replace(/([a-z])([A-Z])/g, "$1 $2").replace(/[\s_-]/g, "-").toLowerCase();
    }
    spinalCase('This Is Spinal Tap');

Things are starting to fall into place :slight_smile: Loving JavaScript more and more everyday!

4 Likes

Hello! This will include all of the possibilities for the algorithm test!

function spinalCase(str) {

  var strToChange = str.split(/(?=[A-Z])|\s(?=[a-z])/)
  
  for (var i = 0; i < strToChange.length; i++) {
strToChange[i] = strToChange[i].toLowerCase().replace(/\s|_/g, '');
  }
  return strToChange.join("-");
}
spinalCase('Teletubbies say Eh-oh The_Andy_Griffith_Show thisIsSpinalTap This Is Spinal Tap AllThe-small Things');