How to find the outlier in an array javascript code wars

preview_player
Показать описание
this explains how to use functions, and comparisons to pull out items from an array that differ from the rest. My code is sort of long, and there may be an issue with negative numbers, but the same stands true for the short code. Mind you this is also a beginner showing how they solve problems for other beginners. My code may be longer but may be easier to digest for new coders :)
Рекомендации по теме
Комментарии
Автор

Here was my solution:

function findOutlier(integers){
const evens = [];
const odds = [];

integers.forEach(a =>{
if (a % 2 == 0){
evens.push(a);
} else {
odds.push(a);
}
})

if (evens.length == 1){
return evens[0];
} else if (odds.length == 1){
return odds[0];
}

return "The array has no outliers"
}

I even put a "no outlier" statement in case a user accidentally put in more than one even or odd that was supposed to be an outlier, hopefully that made it just a tad more dynamic.

Snesgamer
join shbcf.ru