Problem
- Write simple camelCase method for strings.
- All words must have their first letter capitalized without spaces.
- 모든 단어는 공백 없이 첫 글자가 대문자로 시작해야 한다.
Solution 01
function camelCase(str) {
let arr = str.split(' ');
let result = '';
for (let i = 0; i < arr.length; i++) {
result += arr[i] ? arr[i][0].toUpperCase() + arr[i].slice(1) : '';
}
return result;
}
camelCase('hello case'); // HelloCase
camelCase('camel case word'); // CamelCaseWord
Solution 02
function camelCase(str) {
return str.split(' ').map(i => {
return i.charAt(0).toUpperCase() + i.slice(1);
}).join('');
}
camelCase('hello case'); // HelloCase
camelCase('camel case word'); // CamelCaseWord
Solution 03
function camelCase(str) {
return str.split(' ').map(i => i.slice(0, 1).toUpperCase() + i.slice(1)).join('');
}
camelCase('hello case'); // HelloCase
camelCase('camel case word'); // CamelCaseWord