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 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.
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.