Skip to content

Instantly share code, notes, and snippets.

@SarasArya
Created October 7, 2021 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SarasArya/0ed00e68e2a90a2b54472d9404602387 to your computer and use it in GitHub Desktop.
Save SarasArya/0ed00e68e2a90a2b54472d9404602387 to your computer and use it in GitHub Desktop.
Return Product for Even and 0 for Odd JS
// Given a list of integers, determine if the product of all of the integers is even or odd.
// Return the sum of all of the integers if the product of all of the integers is even.
// Return 0 if the product of all of the integers is odd.
// Note that 0 is considered even.
// Examples:
// Input: [1,2,3,4]
// Output: 10
// Explanation: 1 * 2 * 3 * 4 = 24. This is an even number. The sum of all of the integers is 10
// Input: [5,7,9]
// Output: 0
// Explanation: 5 * 7 * 9 = 315 is odd so the function returns 0
export function odd_even_product( my_list ) {
const multiplication = multiplyAllIntegers(my_list);
if(multiplication % 2 === 0){
return sumAllIntegers(my_list);
}
else{
return 0;
}
}
function multiplyAllIntegers(arr){
let multiplication = 1;
for(let item = 0; item<arr.length; item++){
multiplication = multiplication * arr[item];
}
return multiplication;
}
function sumAllIntegers(arr){
let sum = 0;
for(let item = 0; item<arr.length; item++){
sum = sum + arr[item];
}
return sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment