简体   繁体   中英

how to convert passed string to Camel Case

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

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)

Try indexing from 0 instead of 1 in your initializing loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM