简体   繁体   中英

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;
    }
  }
}

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.

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;
    }
}

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

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

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);

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]
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM