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: