how to convert passed string to Camel Case

70 views Asked by At

Hi I'm working on a problem that requires me to 'returns the passed string convertedToCamelCase'

I tried doing it like this

let wordsArr = words.toLowerCase().split(" ")
  for (let i = 1; i<wordsArr.length; i++){
  wordsArr[i] = wordsArr[i].charAt(0).toUpperCase()
  wordsArr.slice(1)
  }
  return wordsArr.join("")

but that doesnt seem to work and now im stuck

1

There are 1 answers

0
Seth Evry On

Something like this should work if it doesn't contain punctuation

let camelot = "I have to push the pram a lot";
const makeCamel = s => {
  let camelArray = s.toLowerCase().split(' ')
  let newArray = [camelArray[0]]
  for (let i in camelArray) {
    if (i >= 1) {
      let capLetter = camelArray[i][0].toUpperCase()
      let rest = camelArray[i].slice(1);
      let newWord = capLetter + rest
      newArray.push(newWord);
      
    }
  }
  return newArray.join('');

}
makeCamel(camelot)