1

I am trying to determine if the product of all the integers is even or odd.

I have a list of integers, and I want to check if the product of the integers is even or odd.

For example:

    determineIfEvenOrOdd([6,7,9,9])

    function determineIfEvenOrOdd(int_array) {

    //Code here

    }

I was thinking of looping through the array and multiplying the numbers to find out the product of the array integers. But then I thought it would be expensive to do this if the array was huge.

I am a beginner, and would like to know a possible approach for the solution.

salasar341
  • 13
  • 4
  • Does this answer your question? [How to find the sum of an array of numbers](https://stackoverflow.com/questions/1230233/how-to-find-the-sum-of-an-array-of-numbers) – Heretic Monkey Feb 08 '21 at 13:29
  • 3
    You just need to check if the array has at least one even number – adiga Feb 08 '21 at 13:30
  • "But then I thought it would be expensive to do this if the array was huge." A more realistic problem would be that JavaScript doesn't have integers, it only has double-precision floating point numbers. From a certain size upwards, not all integers can be exactly represented anymore. @adiga has the right idea here. – Thomas Feb 08 '21 at 13:33
  • What is the product of an even number and an odd number - even or odd? Let's say `4 * 5`. Now, what's the product of two even numbers? And two odd numbers? – VLAZ Feb 08 '21 at 13:33
  • There's a dupe for @adiga's solution too: [Determining if an array contains an even number](https://stackoverflow.com/q/29143070/215552) – Heretic Monkey Feb 08 '21 at 13:38

4 Answers4

2

If your list of numbers contains an even number, the product of all numbers will be even.

You can loop through the list of numbers and check each individual number for even-ness with a conditional statement like:

    // Put this code inside function determineIfEvenOrOdd
    var even = false;
    for (let i=0; i<int_array.length; i++) {
        if (i%2==0) {
            even = true;
            break;
        }
    }
Dan
  • 91
  • 2
1

An efficient way would be to check whether there is any number that is even, if so, the product is even. If there aren't any numbers that are even, then the product is odd.

function isProductEven(arr) 
{ 
    for (let i = 0; i < arr.length; i++) 
        if ((arr[i] & 1) === 0) return true; 
  
    return false; 
} 

console.log(isProductEven([6, 7, 9, 9]))

This outputs true as the product of these numbers is even.

Silidrone
  • 1,471
  • 4
  • 20
  • 35
  • This doesn't seem to work. https://playcode.io/734832/ – salasar341 Feb 08 '21 at 13:38
  • I put a snippet, it actually does, that's just a warning, if you're really pedantic just put `===` instead of `==`. I edited the answer, it will still compile and result the same, though you won't be getting the warning anymore. – Silidrone Feb 08 '21 at 13:39
  • @salasar341 your link produces `true` as a result. This is indeed the correct result. Why do you say it doesn't work? – VLAZ Feb 08 '21 at 13:41
  • @VLAZ Yeah I think he meant the warning that playcode gave him. It's because I used `==` instead of `===`, I edited, but it still worked before the edit, my best guess is he meant there are warnings when compiling by `this doesn't seem to work`. – Silidrone Feb 08 '21 at 13:42
1

The most concise solution would be this:

const isProductEven = arr =>
    arr.some(e=>!(e%2));

This checks if at least one of the numbers in the array is even by testing if its remainder from division by 2 equals to 0. You can use it directly as well:

console.log([1,2,3].some(e=>!(e%2))); // true
console.log([1,3,5].some(e=>!(e%2))); // false
console.log([].some(e=>!(e%2))); // false
Levi Haskell
  • 795
  • 8
  • 20
0

I usually implement Array.reduce to achieve this. It's easy to implement.

function isArrayProductEven(arr) {
  return !(arr.reduce((a, b) => a * b, 1) % 2);
}

console.log(isArrayProductEven([1,2,3,4,5]));
console.log(isArrayProductEven([1,3,5,7,9]));

How this works is: multiply all numbers in the array and check the remainder of 2 (odd / even) to determine if the result is odd (false) or even (true).

Ashwin
  • 541
  • 1
  • 4
  • 7