-
Notifications
You must be signed in to change notification settings - Fork 0
/
DAY_3.js
44 lines (37 loc) · 1015 Bytes
/
DAY_3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const numbers = [50, 60, 60, 45, 75, 88, 70, 72, 42]
function checkArrayNotEmpty(array) {
if (typeof array != "undefined" && array != null && array.length != null
&& array.length > 0)
return true
else
return false
}
function isEvenNumber(aNumber) {
return aNumber%2==0;
}
// function isOddNumber(aNumber) {
// return aNumber%2!=0;
// }
function alternatingSums(numbersArray) {
let sumEvens = 0;
let sumOdds = 0;
const res = [];
if (checkArrayNotEmpty(numbersArray))
for (let index = 0; index < numbersArray.length; index++) {
const element = numbersArray[index];
if (isEvenNumber(index)) {
sumEvens += numbersArray[index]
} else {
sumOdds += numbersArray[index]
}
}
res.push(sumEvens)
res.push(sumOdds)
return res;
}
// act
const answer = alternatingSums(numbers)
for (let index = 0; index < answer.length; index++) {
const element = answer[index];
console.log(element)
}