-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleStringCharacters.js
More file actions
45 lines (40 loc) · 924 Bytes
/
simpleStringCharacters.js
File metadata and controls
45 lines (40 loc) · 924 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// https://www.codewars.com/kata/5a29a0898f27f2d9c9000058/train/javascript
// Solution 1: **Not working
function solve(s) {
let lowerCase = 0;
let upperCase = 0;
let number = 0;
let special = 0;
return s.split("").reduce((char) => {
if (/[A-Z]/.test(char)) {
lowerCase++;
} else if (char.toUpperCase() === char) {
upperCase++;
} else if (typeof parseFloat(char) === "number") {
number++;
} else if (/[\M]/.test(char)) {
special++;
}
});
}
// Solution 2: Working
function solve(s) {
return s.split("").reduce(
(acc, curr) => {
if (/[A-Z]/.test(curr)) {
acc[0] = acc[0] + 1;
}
if (/[a-z]/.test(curr)) {
acc[1] = acc[1] + 1;
}
if (/[\d]/.test(curr)) {
acc[2] = acc[2] + 1;
}
if (/[^a-zA-Z0-9]/.test(curr)) {
acc[3] = acc[3] + 1;
}
return acc;
},
[0, 0, 0, 0]
);
}