본문 바로가기
알고리즘/Codewars

[Codewars] Odd or Even? (7 kyu) / JavaScript

by fluss 2023. 1. 10.

https://www.codewars.com/kata/5949481f86420f59480000e7

 

Codewars - Achieve mastery through coding practice and developer mentorship

A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!

www.codewars.com

 

DESCRIPTION:

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!

 

코드

function oddOrEven(array) {
  let countOdd = 0;
  for(let i = 0; i < array.length; i++){
    if(array[i] % 2 === 1 || array[i] * -1 % 2 === 1) countOdd++;
  }
  return countOdd % 2 === 1? 'odd' : 'even';
}

댓글