Spinal Tap Case - feedback

Tell us what’s happening:
The thing worked. It ain’t necessarily pretty, for example checking for capital letters two different times could probably be consolidated, but it works and it took a while.

Anyone mind offering a bit of feedback?

Your code so far



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

let split = [];
split = str.split(/[\W_]/g);//split the string on non-word characters
split = stringify(split);//call stringify function on split array of strings
split = split.join("-").toLowerCase();//join the array and make lowercase
return split;
}

console.log(spinalCase("ThisIs-Spinal Tapper Spinal"));

function stringify(s){
let sliced = s.slice(s);// no mutation
let removed = [];
let capIndex = [0];//pre-load with the first character to ignore case
for (let i = 0; i < sliced.length; i++){
  for (let j = 0; j < sliced[i].length; j++){//two loops to check for individual character matches inside each element of the array of strings
    if (sliced[i][j].match(/[A-Z]/) != null && j != 0) {//check for a capital letter anywhere in the current element's string and store the index location. don't worry about first index location as we know that's the start and pre-loaded
      capIndex.push(j);
    }
  }
  if (sliced[i].match(/^.+[A-Z].*$/) != null) {//check for a capital in the middle of the current element's string
    for (let k = 0; k < capIndex.length; k++){
      removed.push(sliced[i].slice(capIndex[k], capIndex[k+1]));//if so, slice out the individual words using our index and then push
    } 
  } else {
      removed.push(sliced[i]);//if not, push
  } 
}
return removed;
}


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0.

Challenge: Spinal Tap Case

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

  1. There are quite a lot “non-word” characters in words sometimes, like: I've lost $45 :( #Broke
  2. There are also some all cap words, like HTML

:wink:

You should have a look at some of the other solutions shown on the Get a Hint link and compare them to yours to see how things could be done differently.

Hah. Good points. Figured it was easily broken. Thanks for the tips. :blush:

Wow. It is SO much easier than I made it out to be. Thanks for the direction. :joy: