-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7kyuCheckThreeAndTwo.js
41 lines (36 loc) · 1.01 KB
/
7kyuCheckThreeAndTwo.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
//Given an array with exactly 5 strings "a", "b" or "c", check if the array contains three and two of the same values.
function checkThreeAndTwo(array) {
let countA = 0;
let countB = 0;
let countC = 0;
for (let i = 0; i < array.length; i++) {
if (array[i] === "a") {
countA++
} else if (array[i] === "b") {
countB++
} else if (array[i] === "c") {
countC++
}
}
if ((countA === 3 && countB === 2) ||
(countA === 3 && countC === 2) ||
(countB === 3 && countC === 2) ||
(countB === 3 && countA === 2) ||
(countC === 3 && countA === 2) ||
(countC === 3 && countB === 2)) {
return true;
} else {
return false;
}
}
//solution2
function checkThreeAndTwo(array) {
const counts = { a: 0, b: 0, c: 0 };
for (let char of array) {
if (counts.hasOwnProperty(char)) {
counts[char]++;
}
}
const values = Object.values(counts);
return (values.includes(3) && values.includes(2));
}