How remove empty elements from array?

function spinalCase(str) {
  // "It's such a fine line between stupid, and clever."
  // --David St. Hubbins
  let newStr= str.replace(/([A-Z])/g, ' $1').trim();
  console.log(newStr);
   let newStr2= str.replace(/([A-Z])/g, ' $1').trim().toLowerCase().split(" ")
     console.log(newStr2);
     var filtered = newStr2.filter(function (el) {
  return el != null;
});
console.log(filtered)

   let newStr3 = newStr2.join("-");
       console.log(newStr3);
  return newStr3;
}

spinalCase('This Is Spinal Tap');

Link : https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/spinal-tap-case
How remove empty elements from array in :

     var filtered = newStr2.filter(function (el) {
  return el != null;
});

The way I solved this one was with by splitting the string using regEx, on any whitespace \s or underscores _, followed by a positive lookahead (?=[ for any uppercase characters ranging from A-Z])
str.split(/\s|_|(?=[A-Z])/)

Then I joined them back together together using .join('-'), placing a hyphen between them. And finally I used the .toLowerCase() method to convert all characters to lowercase.

const spinalCase =str => str.split(/\s|_|(?=[A-Z])/).join('-').toLowerCase()

Thank you :slightly_smiling_face:

You are welcome :slight_smile: Why does this have to be 20 or more characters? lol

function spinalCase(str) {
  // "It's such a fine line between stupid, and clever."
  // --David St. Hubbins
  let newStr= str.replace(/([A-Z])/g, ' $1').trim();
  console.log(newStr);
   let newStr2= str.replace(/([A-Z])/g, ' $1').trim().toLowerCase().split(" ")
     console.log(newStr2);
     var filtered = newStr2.filter(number => number )
console.log(filtered)
    
   let newStr3 = filtered.join("-");
       console.log(newStr3);
  return newStr3;
}

spinalCase('The_Andy_Griffith_Show');

And how to remove “_” from string in my case ? Using regexp?

/_/g

Here is a very helpful tool: https://regexr.com/

Thank you :slightly_smiling_face: