-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexes_and_ohs.js
More file actions
41 lines (33 loc) · 816 Bytes
/
exes_and_ohs.js
File metadata and controls
41 lines (33 loc) · 816 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
/*
codewars.com/kata/55908aad6620c066bc00002a
Check to see if a string has the same amount of 'x's and 'o's. The method must return a boolean and be case insensitive. The string can contain any char.
Examples input/output:
XO("ooxx") => true
XO("xooxx") => false
XO("ooxXm") => true
XO("zpzpzpp") => true // when no 'x' and 'o' is present should return true
XO("zzoo") => false
*/
// First Attempt - Feb 2019
function XO(str) {
let x = 0;
let o = 0;
str = str.toLowerCase();
str = str.split('');
for (let i of str) {
i == 'x' ? x++ : i == 'o' ? o++ : undefined;
}
return x == o;
}
// Second Attempt - Feb 2020
function XO(str) {
let count = 0;
for (char of str) {
if (char.toLowerCase() === 'x') {
count--;
} else if (char.toLowerCase() === 'o') {
count++;
}
}
return count === 0;
}