-2

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
    Hello. Did you look here- https://stackoverflow.com/questions/2970525/converting-any-string-into-camel-case – manishk Nov 14 '19 at 23:52
  • Possible duplicate of [Converting any string into camel case](https://stackoverflow.com/questions/2970525/converting-any-string-into-camel-case) – Ivar Nov 14 '19 at 23:52
  • I think you should use a dictionary with predefined words to achieve that, cause there is no way you can detect end of words without knowing them in advance – Mehdi Benmesssaoud Nov 14 '19 at 23:52

1 Answers1

1

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)
Seth Evry
  • 11
  • 2