Intermediate Algorithm Scripting - Spinal Tap Case

Tell us what’s happening:
It seems that my code just won’t get into the if conditions, hence why I won’t get any console.logs there…
The idea of the if conditions is that I look for any uppercase letter, with it’s predecessor being a lower case letter, and add a - between those two letters. I used slice for it and used the tmp variable for it
Your code so far

function spinalCase(str) {
  const strArr = str.split(/\W|_/)
  // console.log(strArr)
  var resArr = []
  return strArr.reduce((resStr, elem) => {
    for(let i = 1; i<elem.length; i++){
      if(elem.charAt(i) == /[A-Z]/){
        console.log("HELLO!")
        if(elem.charAt(i-1) == /[a-z]/){
          console.log("Replacing!!!")
          var tmp1 = elem
          elem = tmp1.slice(0,i) + "-" + tmp1.slice(i)
        }
      }
    }
    
    resArr.push(elem.toLowerCase())
    resStr = resArr.join("-");
    // console.log(resArr)
    // console.log(resStr)
    return resStr;
  }, "")
}

spinalCase('AllThe-small Things');

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.41

Challenge: Intermediate Algorithm Scripting - Spinal Tap Case

Link to the challenge:

You are checking if a character in a string is equal to a regular expression. Those are different data types. They will never be equal. You might want to research how to use regular expressions to test strings.

1 Like

thank you!!! I managed it somehow to do it with test() :sweat_smile:

function spinalCase(str) {
  const strArr = str.split(/\W|_/)
  
  var resArr = []
  return strArr.reduce((resStr, elem) => {
    for(let i = 1; i<elem.length; i++){
      if(/[A-Z]/.test(elem.charAt(i))){
        if(/[a-z]/.test(elem.charAt(i-1))){
          var tmp1 = elem
          elem = tmp1.slice(0,i) + "-" + tmp1.slice(i)
        }
      }
    }
    
    resArr.push(elem.toLowerCase())
    resStr = resArr.join("-");
    
    return resStr;
  }, "")
}

spinalCase('AllThe-small Things');

Certainly not efficient but it works I guess :sweat_smile: