[Solved] A function that returns the number of even numbers of an array


One actually does never want to mix computation logic with whatever kind of output code.

In addition one could make use of Array.prototype.filter and a precise filter condition which does target odd and/or even number values … something similar to the next provided example code …

function getEvenCount(list) {
  return list.filter(item =>
    (typeof item === 'number') && (item % 2 === 0)
  ).length;
}
function getOddCount(list) {
  return list.filter(item =>
    (typeof item === 'number') && (item % 2 !== 0)
  ).length;
}

document.write(
  'getEvenCount([2,4,8,7]) ... ' +
  getEvenCount([2,4,8,7]) +
  '</br>'
)
document.write(
  'getEvenCount([1,9,66,"banana"]) ... ' +
  getEvenCount([1,9,66,"banana"]) +
  '</br>'
)
document.write(
  'getOddCount([2,4,8,7]) ... ' +
  getOddCount([2,4,8,7]) +
  '</br>'
)
document.write(
  'getOddCount([1,9,66,"banana"]) ... ' +
  getOddCount([1,9,66,"banana"]) +
  '</br>'
)

console.log(
  'getEvenCount([2,4,8,7]) ... ',
  getEvenCount([2,4,8,7])
);
console.log(
  'getEvenCount([1,9,66,"banana"]) ...',
  getEvenCount([1,9,66,"banana"])
);
console.log(
  'getOddCount([2,4,8,7]) ...',
  getOddCount([2,4,8,7])
);
console.log(
  'getOddCount([1,9,66,"banana"]) ...',
  getOddCount([1,9,66,"banana"])
);

0

solved A function that returns the number of even numbers of an array