I developed a function that can identify the outlier in an array consisting of a set of odd numbers and one even number, or vice versa. For example, findOutlier([2,6,8,10,3])
will return 3 as it is the only odd number in the array.
Although I have successfully implemented this function, I encountered an issue with handling certain large negative numbers. It seems to return undefined instead of the expected outlier number.
Below is the code snippet:
function findOutlier(integers){
let testingForOdds = true;
let evenCounter = 0;
let oddCounter = 0;
for (let i = 0; i < 3; i++){
if (integers[i] % 2 === 0){
evenCounter = evenCounter + 1
if (evenCounter === 2){
testingForOdds = true;
}
}
else if (integers[i] % 2 === 1){
oddCounter = oddCounter + 1
if (oddCounter === 2){
testingForOdds = false;
}
}
}
if (testingForOdds){
for (let i = 0; i < integers.length; i++){
if (integers[i] % 2 === 1){
return integers[i]
}
}
} else {
for (let i = 0; i < integers.length; i++){
if (integers[i] % 2 === 0){
return integers[i]
}
}
}
}
findOutlier([-100000000007, 2602, 36]);
Despite working flawlessly on examples like findOutlier([2,6,8,10,3])
, the function shows unexpected behavior with negative values such as in
findOutlier([-100000000007, 2602, 36]);
. Instead of returning the correct result, it returns undefined. What could be causing this discrepancy?