알고리즘 70 - Convert string to camel case

박진현·2021년 7월 22일
0

Q.

Description:
Complete the method/function so that it converts dash/underscore delimited words into camel casing. The first word within the output should be capitalized only if the original word was capitalized (known as Upper Camel Case, also often referred to as Pascal case).

Examples
"the-stealth-warrior" gets converted to "theStealthWarrior"
"The_Stealth_Warrior" gets converted to "TheStealthWarrior"

A)

function toCamelCase(str){
  let res = '';
  for (let i=0; i<str.length; i++) {
    if(str[i] === '-' || str[i] === '_') {
      res += str[i+1].toUpperCase()
      i++
    }
    else {
        res += str[i]
      }
  }
  return res
}
profile
👨🏻‍💻 호기심이 많고 에러를 좋아하는 프론트엔드 개발자 박진현 입니다.

0개의 댓글