Intermediate Algorithm Scripting - Spinal Tap Case

hi there wondering if i can do this better soi dont have to have the

if(arr[0]===’ '){
arr.shift();
}

the challange :https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/spinal-tap-case

here is the code

function spinalCase(str) {
  
  let str1= str.replace(/[A-Z]/g, function(v) {return ' '+ v.toLowerCase();
});
let arr=str1.split(/[\s,._]+/);
console.log(arr);
console.log(arr[0]);
if(arr[0]===''){
  arr.shift();
}
let str2=arr.join('-')

 
  console.log(str2)
  return str2;
}

spinalCase("The_Andy_Griffith_Show");

Try:

  1. Split the string with regex . It will return an array
  2. join the array with (’ - '). it will return a string
  3. lower case the string

You can chain all of that method together

hope this help

happy coding

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.