-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertstringtocamelcase.js
More file actions
26 lines (20 loc) · 963 Bytes
/
Copy pathConvertstringtocamelcase.js
File metadata and controls
26 lines (20 loc) · 963 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 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). The next words should be always capitalized.
// Examples
// "the-stealth-warrior" gets converted to "theStealthWarrior"
// "The_Stealth_Warrior" gets converted to "TheStealthWarrior"
// "The_Stealth-Warrior" gets converted to "TheStealthWarrior"
function toCamelCase(str){
return str.replace(/[_-]/g, " ").split(' ').map((word, i) => i === 0 ? word : word.slice(0, 1).toUpperCase() + word.slice(1)).join('')
}
//different approach
function toCamelCase(str){
let arr = str.split('');
for(i = 0; i < arr.length; i++){
let letter = arr[i];
if(letter == '_' || letter == '-') {
arr[i + 1] = arr[i + 1].toUpperCase();
arr[i] = '';
}
}
return arr.join('');
}