-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
iq-test.js
45 lines (40 loc) · 1.13 KB
/
iq-test.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
// iqTest("2 4 7 8 10") => 3 // Third number is odd, while the rest of the numbers are even
// iqTest("1 2 1 1") => 2 // Second number is even, while the rest of the numbers are odd
function iqTest(numbers){
numbers = numbers.split(' ');
// keep track of even count, and the last index we saw of even
const even = {
count: 0,
lastIndex: -1
};
// keep track of odd count, and the last index we saw of odd
const odd = {
count: 0,
lastIndex: -1
};
// iterate over numbers
for (let i = 0; i < numbers.length; i++) {
const currentNumber = numbers[i];
// if even, increment even count and store index
if (currentNumber % 2 == 0) {
even.count++;
even.lastIndex = i + 1;
} else {
// if odd, increment odd count and store index
odd.count++;
odd.lastIndex = i + 1;
}
}
// if even count is 1
if (even.count == 1) {
// return the last even index we saw
return even.lastIndex;
}
// else if the odd count is 1
else {
// return the last odd index we saw
return odd.lastIndex;
}
}
console.log(iqTest("2 4 7 8 10"), 3);
console.log(iqTest("1 2 2"), 1);