CoderAz
CoderAz

Reputation: 99

Returning an odd or even number from array

Just need help in identifying what I am doing wrong on this codewar challenge.

I realize this may be easy for some but please note I am just a beginner with Javascript.

The challenge:

You are given an array (which will have a length of at least 3, but could be very large) containing integers. The array is either entirely comprised of odd integers or entirely comprised of even integers except for a single integer N. Write a method that takes the array as an argument and returns N.

For example:

[2, 4, 0, 100, 4, 11, 2602, 36] should return 11.

[160, 3, 1719, 19, 11, 13, -21] should return 160.

My code:

function findOutlier(integers){

    var even = [];
    var odd = [];

  for (var i = 0; i < integers; i++) {
    if (integers[i] % 2 === 0) {
        even.push(integers[i]);
    } else {
        odd.push(integers[i]);
    }

    if (even.length === 1) {
        return even;
    } else {
        return odd;
    }
  }
}

Upvotes: 1

Views: 9779

Answers (5)

lowkey loki
lowkey loki

Reputation: 1

push the odd and the even results into an array, and then compare what you have left

function findOutlier(integers) {
  var arrO=[];
 var arrE=[];
    for(i=0; i<integers.length; i++){
if(integers[i] % 2==0 ){
arrE.push(integers[i])
}
  else {
arrO.push(integers[i])
    }
    }
    if(arrO.length > arrE.length){
  return arrE[0]
    }return arrO[0]
   }

Upvotes: 0

Redu
Redu

Reputation: 26161

If you want to get the first occurrence without iterating to the last item but without using an imperative code you may do as by using .find() checking inside if the rightmost bit of n is 1;

var arr     = [2, 4, 0, 100, 4, 11, 2602, 36],
    resodd  = arr.find(n => n & 1),
    reseven = arr.find(n => !(n & 1));

console.log(resodd, reseven);

Upvotes: 0

Dan Kreiger
Dan Kreiger

Reputation: 5516

Another possible way:

function myFunction(integers) {
  var odds = integers.filter(function(num) {return num % 2});
  var evens = integers.filter(function(num) {return !(num % 2)});
  return evens.length == 1 ? evens[0] : odds[0];
}

You can check out this CodePen Demo to test the function in Mocha.

Upvotes: 1

Ralph
Ralph

Reputation: 1

Try: for (var i = 0; i < integers.length; i++)

instead of: for (var i = 0; i < integers; i++)

Upvotes: 0

Md. Mehedi Hasan
Md. Mehedi Hasan

Reputation: 196

I've found 2 issues inside your code block. You have to run loop over array length instead of entire array otherwise you can use foreach loop. You need to return odd/even value after finishing your loop. Please check updated code as follows, hope it'll help.

function findOutlier(integers){

    var even = [];
    var odd = [];

    for (var i = 0; i < integers.length; i++) {
        if (integers[i] % 2 === 0) {
            even.push(integers[i]);
        } else {
            odd.push(integers[i]);
        }
    }
    if (even.length === 1) {
        console.log("OK..1");
        return even;
    } else {
        console.log("OK..2");
        return odd;
    }
}

Upvotes: 0

Related Questions