-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchar-counter-algorithm.ts
46 lines (32 loc) · 1.15 KB
/
char-counter-algorithm.ts
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function stringCharCounter(input: string) {
let result: any = {};
if (!input) throw new Error("The list is empty");
for (let char of input) {
let loweredChar = char.toLowerCase();
let validAlphanumericalRegexp = /[^a-z0-9]/gi;
if (!validAlphanumericalRegexp.test(loweredChar)) {
result[loweredChar] = ++result[loweredChar] || 1;
}
}
return result;
}
console.log(stringCharCounter("cccan ")); //output : {c:3, a:1, n:1}
// function stringCharCounter(input: string) {
// let result: any = {};
// if (!input) throw new Error("The list is empty");
// for (let char of input) {
// let loweredChar = char.toLowerCase();
// let validAlphanumericalRegexp = /[^a-z0-9]/gi;
// result[loweredChar] && !validAlphanumericalRegexp.test(char)
// ? ++result[loweredChar]
// : (result[loweredChar] = 1);
// if (result[loweredChar] && !validAlphanumericalRegexp.test(char)) {
// ++result[loweredChar];
// } else {
// result[char] = 1;
// }
// }
// return result;
// }
// console.log(stringCharCounter("cccan")); //output : {c:3, a:1, n:1}
//Isn't refactored