The IQ test gives an array of numbers.
One of them differs from the others by parity / oddness.
The function returns its index.
function iqTest(str) { array = str.split(' '); var boolArray = []; function checkEven(num) { return +num % 2 === 0? true:false; } for (var i = 0; i < array.length; i++) { boolArray.push(checkEven(array[i])); } var differ = boolArray[0]; var index = 1; for (var j = 1; j < boolArray.length; j++) { if (boolArray[j] !== differ) { differ = boolArray[j]; index = j; } } return index; }; But the function returns an invalid index, when a different element is an even number.
diffcome from?if (boolArray[j] !== dif)- ThisMan? true : false? true : falseis useless in this case. 2. No need to bet;afterfor {}andfunction {}, but afterreturns and other lines inside blocks of code you need. - Regent?and so the expression returnstrueorfalse, and this value matches what you return - Grundy