알고리즘 79 - Odd or Even?

jabae·2021년 11월 1일
0

알고리즘

목록 보기
79/97

Q.

Task:

Given a list of integers, determine whether the sum of its elements is odd or even.

Give your answer as a string matching "odd" or "even".

If the input array is empty consider it as: [0] (array with a zero).

Examples:

Input: [0]
Output: "even"

Input: [0, 1, 4]
Output: "odd"

Input: [0, -1, -5]
Output: "even"
Have fun!

A)

function oddOrEven(array) {
   return (Math.abs(array.reduce((sum, cur) => sum + cur, 0)) % 2 === 1) 
     								? 'odd' : 'even' 
}
profile
it's me!:)

0개의 댓글