-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
find-the-capitals.js
102 lines (90 loc) · 2.11 KB
/
find-the-capitals.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
function capitals(word) {
// a place to store the indices
const indices = [];
// iterate over the word
for (let i = 0; i < word.length; i++) {
const letter = word[i];
// if the current letter is uppercase
if (letter === letter.toUpperCase()) {
// push the current index into the indices array
indices.push(i);
}
}
// return indices array
return indices;
}
function capitals(word) {
return word.split('').map((letter, i) => ({
letter,
i
}))
.filter(({letter}) => letter === letter.toUpperCase())
.map(({i}) => i);
}
function capitals(word) {
const indices = [];
for (let i = 0; i < word.length; i++) {
const charCode = word.charCodeAt(i);
if (charCode >= 65 && charCode <= 90) {
indices.push(i);
}
}
return indices;
}
function capitals(word) {
return word.split('').map((letter, i) => ({
charCode: letter.charCodeAt(0),
i
}))
.filter(({charCode}) => charCode >= 65 && charCode <= 90)
.map(({i}) => i);
}
function capitals(word) {
const indices = [];
for (let i = 0; i < word.length; i++) {
const letter = word[i];
if (letter.match(/[A-Z]/)) {
indices.push(i);
}
}
return indices;
}
function capitals(word) {
return word.split('').map((letter, i) => ({
letter,
i
}))
.filter(({letter}) => letter.match(/[A-Z]/))
.map(({i}) => i);
}
// foreach
function capitals(word) {
const indices = [];
word.split('').forEach((letter, i) => {
if (letter.match(/[A-Z]/)) {
indices.push(i);
}
});
return indices;
}
// reduce
function capitals(word) {
return word.split('').reduce((indices, letter, i) => {
if (letter.match(/[A-Z]/)) {
indices.push(i);
}
return indices;
}, []);
}
function capitals(word) {
return Array.prototype.reduce.call(word, (indices, letter, i) => {
if (letter.match(/[A-Z]/)) {
indices.push(i);
}
return indices;
}, []);
}
function capitals(word) {
return Array.prototype.reduce.call(word, (indices, letter, i) => letter.match(/[A-Z]/) ? indices.concat(i) : indices, []);
}
console.log( capitals('CodEWaRs'), [0,3,4,6] );