0

Wondering why the second function doesn’t work, do I have to redefine str every time instead of just run .replace() or toUpperCase() on str?

function toCamelCase(str){
for(let i = 0; i < str.length - 1; i ++) {
  if (str[i] === '-' || str[i] === '_') {
    str = str.replace(str[i], '')
    str = str.replace(str[i], str[i].toUpperCase())
  }
}
return str
} 


function toCamelCase(str){
  for(let i = 0; i < str.length - 1; i ++) {
    if (str[i] === '-' || str[i] === '_') {
       str.replace(str[i], '')
       str.replace(str[i], str[i].toUpperCase())
    }
  }
  return str
  } 
  • `do I have to redefine str every time instead of just run .replace() or toUpperCase() on str?` you need to assign result of replacement somewhere and `str` looks like good candidate – Iłya Bursov Oct 12 '22 at 03:04

0 Answers0