Why does not the return return the correct value?

0

I want to return the value of console , but it does not return anything.

This is my code:

function findOutlier(integers) {
  let odd = integers[0];

  integers.forEach(function(current) {

    if (odd % 2 == 0) {
      if (current % 2 > 0) {
        console.log(current);// ESTE VALOR
        return current;
      }
    } else {
      if (current % 2 == 0) {
        return current;
      }
    }
  });
}

console.log(findOutlier([2, 6, 8, 10, 3]));

My exercise is:

  

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 to method that takes the array as   an argument and returns this "outlier" N.

     

Examples [2, 4, 0, 100, 4, 11, 2602, 36] Should return: 11 (the only   odd number)

     

[160, 3, 1719, 19, 11, 13, -21] Should return: 160 (the only even   number)

I try to get these results:

Test.assertEquals(findOutlier([0, 1, 2]), 1)
Test.assertEquals(findOutlier([1, 2, 3]), 2)
Test.assertEquals(findOutlier([2,6,8,10,3]), 3)
Test.assertEquals(findOutlier([0,0,3,0,0]), 3)
Test.assertEquals(findOutlier([1,1,0,1,1]), 0)
    
asked by hubman 11.01.2018 в 16:49
source

2 answers

3

The forEach method, is useful for:

  

execute the indicated function once for each element of the array.
Return value : undefined .

Solution:

To get only the first result that meets the condition you could use find

Example:

function findOutlier(integers) {
  let odd = integers[0];

  return integers.find(function(current) {

    if (odd % 2 == 0) {
      if (current % 2 > 0) {
        return current;
      }
    } else {
      if (current % 2 == 0) {
        return current;
      }
    }
  }) || 0; // Si no encontramos ninguno
}

console.log(findOutlier([2, 6, 8, 10, 3]), 3);
console.log(findOutlier([0, 1, 2]), 1)
console.log(findOutlier([1, 2, 3]), 2)
console.log(findOutlier([2,6,8,10,3]), 3)
console.log(findOutlier([0,0,3,0,0]), 3)
console.log(findOutlier([1,1,0,1,1]), 0);

Update

As commented by @egallardo, your method does not work for all cases.
This could be a better solution:

function findOutlier(integers) {
  // Buscamos todos los numeros pares
  let even = integers.filter(a => a % 2 === 0);
  // Si solo encontramos 1, lo devolvemos, sino buscamos el impar
  return even.length === 1 ? even[0] : integers.find(a => a % 2);
}

console.log(findOutlier([2, 6, 8, 10, 3]), 3);
console.log(findOutlier([0, 1, 2]), 1)
console.log(findOutlier([1, 2, 3]), 2)
console.log(findOutlier([2, 6, 8, 10, 3]), 3)
console.log(findOutlier([0, 0, 3, 0, 0]), 3)
console.log(findOutlier([1, 1, 0, 1, 1]), 0);
console.log(findOutlier([9,2,8,6,4]), 9); // Caso comentado
    
answered by 11.01.2018 / 17:03
source
1

This is shorter and works in all cases.

var findOutlier = (arr, f = 0) => {
 arr.forEach(c => c%2 === 0 ? ++f : f);
 return f > 1 ? arr.find(n => n%2 !== 0) : arr.find(n => n%2 === 0);
};



console.log(findOutlier([160, 3, 1719, 19, 11, 13, -21]));
console.log(findOutlier([9,2,8,6,4]));
console.log(findOutlier([2, 4, 0, 100, 4, 11, 2602, 36]));
    
answered by 11.01.2018 в 19:28