Why my Code doesnt past the tests?

Tell us what’s happening:
Hello there anyone can explain me why this code doesnt pass the tests? It do what it is suposed to do. At least thats what i think :stuck_out_tongue: Ty in advance!
Your code so far


function spinalCase(str) {
let arr = str.split(/[-.,@#$%^&*()_+=#\s\t~#`;'><!]+/)
arr.forEach((e, index)=>{
  for (let i=1; i<e.length;i++) {
    if (e[i].match(/[A-Z]/)) {
      arr.splice(index+1,0,e.slice(i))
      arr[index] = e.slice(0,i)
    }
}
})
return arr.map((e, index)=>{
  return arr.indexOf(e)!==arr.length-1 ? e.toLowerCase() +"-": e.toLowerCase()
}).join("")
}

console.log(spinalCase('Teletubbies say Eh-oh'));
  **Your browser information:**

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

Challenge: Spinal Tap Case

Link to the challenge:

Hi @bill32play. Your regex isn’t matching the second test case, “thisIsSpinalTap”.

My regex is a little rusty… but you can use this handy tool to test and debug your regex: regex101: build, test, and debug regex

Cheers~

Your function returns thisisspinal-tap-spinaltap-isspinaltap instead of this-is-spinal-tap for the second test.

Thank u alot for responding and helped me. actually i test all the examples except from that . i achieve to solve the problem. it seem a complcated solution. pretty sure it can be solved with less code. TY again . u doing a great job.
Here is my new solution:

function spinalCase(str) {

  let arr = str.split(/[-.,@#$%^&*()_+=#\s\t~#`;'><!]+/)

  let finalArr = []

  //console.log(arr)

  arr.forEach((e, index)=>{

    let lastCindex = 0;//keep track of last seen capital letter

    for (let i=1; i<e.length;i++) {

      if (e[i].match(/[A-Z]/) && i!==e.length-1) {

        finalArr.push(e.slice(lastCindex,i))//found one word

        lastCindex = i;//refresh last cap letter seen

      }

      //console.log(finalArr)

    }

    finalArr.push(e.slice(lastCindex))//if we reach in end we push the last word

  })

  return finalArr.map((e, index)=>{

    return finalArr.indexOf(e)!==finalArr.length-1 ? e.toLowerCase() +"-": e.toLowerCase()

  }).join("")

}

console.log(spinalCase('This Is Spinal Tap'));

Congrats on solving the problem. I added spoiler tags around your code for those who have not worked on the problem yet.

I have also edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Oh yea forgot about spoils! My bad! Thx for the tip :blush:

Στις Παρ, 26 Μαρ 2021, 17:37 ο χρήστης Jessica Wilkins via The freeCodeCamp Forum <freecodecamp@discoursemail.com> έγραψε:

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