-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
exes-and-ohs.js
39 lines (37 loc) · 969 Bytes
/
exes-and-ohs.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
function XO(str) {
let xCount = 0;
let oCount = 0;
const xRegexp = new RegExp('x', 'i');
const oRegexp = new RegExp('o', 'i');
for (let i = 0; i < str.length; i++) {
const letter = str[i];
if (letter.match(xRegexp)) {
xCount++;
} else if (letter.match(oRegexp)) {
oCount++;
}
}
return xCount == oCount;
}
function XO(str) {
if (!str) return true;
const xRegexp = new RegExp('x', 'i');
const oRegexp = new RegExp('o', 'i');
return str.split('').reduce(({xCount, oCount}, letter, index) => {
if (letter.match(xRegexp)) {
xCount++;
} else if (letter.match(oRegexp)) {
oCount++;
}
if (index < str.length - 1) {
return { xCount, oCount };
} else {
return xCount === oCount;
}
}, { xCount: 0, oCount: 0 });
}
console.log(XO('xo') == true);
console.log(XO("xxOo") == true);
console.log(XO("xxxm") == false);
console.log(XO("Oo") == false);
console.log(XO("ooom") == false);