-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamelizer.js
More file actions
31 lines (27 loc) · 876 Bytes
/
Camelizer.js
File metadata and controls
31 lines (27 loc) · 876 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
27
28
29
30
31
/*
Write the function camelize(str) that changes dash-separated words like “my-short-string”
into camel-cased “myShortString”.
That is: removes all dashes, each word after dash becomes uppercased.
Examples:
camelize("background-color") == 'backgroundColor';
camelize("list-style-image") == 'listStyleImage';
camelize("-webkit-transition") == 'WebkitTransition';
*/
//Using regular for loop
const camelize = (str) => {
strArr = str.split("-");
ans = [];
ans.push(strArr[0]);
for (let i = 1; i < strArr.length; i++) {
newWord = strArr[i][0].toUpperCase() + strArr[i].slice(1);
ans.push(newWord)
}
return ans.join("")
}
//Using Es6 methods
const _camelize = (str) => {
ans = str.split("-")
.map((word, index, arr) => word == arr[0] ? word : word[0].toUpperCase() + word.slice(1))
.join("");
return ans
}