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';
}
'알고리즘 > Codewars' 카테고리의 다른 글
[Codewars] Opposites Attract (8 kyu) / JavaScript (0) | 2023.01.12 |
---|---|
[Codewars] Sort the odd (6 kyu) / JavaScript (0) | 2023.01.11 |
[Codewars] Valid Parentheses (5 kyu) / JavaScript (0) | 2023.01.09 |
[Codewars] Find the missing letter (6 kyu) / JavaScript (0) | 2023.01.07 |
[Codewars] Split Strings (6 kyu) / JavaScript (0) | 2023.01.06 |
댓글