freeCodeCamp Challenge Guide: Spinal Tap Case

freeCodeCamp Challenge Guide: Spinal Tap Case
0

Spinal Tap Case


Problem Explanation

Convert the given string to a lowercase sentence with words joined by dashes.

Relevant Links


Hints

Hint 1

Create a regular expression for all white spaces and underscores.

Hint 2

You will also have to make everything lowercase.

Hint 3

The tricky part is getting the regular expression part to work, once you do that then just turn the uppercase to lowercase and replace spaces with underscores using replace().


Solutions

Solution 1 (Click to Show/Hide)
function spinalCase(str) {
  // Create a variable for the white space and underscores.
  var regex = /\s+|_+/g;

  // Replace low-upper case to low-space-uppercase
  str = str.replace(/([a-z])([A-Z])/g, "$1 $2");

  // Replace space and underscore with -
  return str.replace(regex, "-").toLowerCase();
}

// test here
spinalCase("This Is Spinal Tap");

Code Explanation

  • regex contains the regular expression /\s+|_+/g, which will select all white spaces and underscores.
  • The first replace() puts a space before any encountered uppercase characters in the string str so that the spaces can be replaced by dashes later on.
  • While returning the string, another replace() replaces spaces and underscores with dashes using regex.
Solution 2 (Click to Show/Hide)
function spinalCase(str) {
  // Replace low-upper case to low-space-uppercase
  str = str.replace(/([a-z])([A-Z])/g, "$1 $2");
  // Split on whitespace and underscores and join with dash
  return str
    .toLowerCase()
    .split(/(?:_| )+/)
    .join("-");
}

// test here
spinalCase("This Is Spinal Tap");

Code Explanation

  • Similar to the first solution, the first replace() puts a space before any encountered uppercase characters in the string str so that the spaces can be replaced by dashes later on.
  • Instead of using replace() here to replace whitespace and underscores with dashes, the string is split() on the regular expression /(?:_| )+/ and join()-ed on -.

Relevant Links

Solution 3 (Click to Show/Hide)
function spinalCase(str) {
  // "It's such a fine line between stupid, and clever."
  // --David St. Hubbins

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

Code Explanation

  • Split the string at one of the following conditions (converted to an array)
    • a whitespace character [\s] is encountered
    • underscore character [_] is encountered
    • or is followed by an uppercase letter [(?=[A-Z])]
  • Join the array using a hyphen (-)
  • Lowercase the whole resulting string

Relevant Links

10 Likes

So this is what I came up with myself (I am not great with regular expressions)

function spinalCase(str) {
  // "It's such a fine line between stupid, and clever."
  // --David St. Hubbins
  str = str.replace(/[^a-zA-Z0-9-]|[a-z][A-Z]/g, function(x) {
    if (x.match(/[a-z][A-Z]/)) {
        return x[0] + "-" + x[1];
        } else {
        return "-";
        }
  });
  return str.replace(/[A-Z]/g, function(x){
    return x.toLowerCase();
  });
}
1 Like

@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.

It can be done in a single line with:

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

21 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:

4 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 …

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.

4 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.

1 Like

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:

2 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()
}

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');

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');

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");
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: