-
Notifications
You must be signed in to change notification settings - Fork 3
/
IQ_Test.js
48 lines (38 loc) · 1.32 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
46
47
48
// Kata link:
// https://www.codewars.com/kata/552c028c030765286c00007d
// -------------------------------------
// Instructions:
/*
Bob is preparing to pass IQ test. The most frequent task in this test
is to find out which one of the given numbers differs from the others.
Bob observed that one number usually differs from the others in evenness.
Help Bob — to check his answers, he needs a program that among the given
numbers finds one that is different in evenness, and return a position
of this number.
! Keep in mind that your task is to help Bob solve a real IQ test, which
means indexes of the elements start from 1 (not 0)
##Examples :
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
*/
// -------------------------------------
// Solution:
function iqTest(numbers){
let odd = [];
let even = [];
let ar = numbers.split(' ');
for (let i = 0; i < ar.length; i++) {
if (ar[i] % 2 == 0) {
even.push(i + 1);
} else {
odd.push(i + 1);
}
}
return odd.length > even.length? even[0]: odd[0];
}
// -------------------------------------
// Basic Tests
console.log(iqTest("2 4 7 8 10"),3);
console.log(iqTest("1 2 2"), 1);