-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountSum.js
43 lines (33 loc) · 1.1 KB
/
countSum.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
// 8kyu Count of positives / sum of negatives
function countPositivesSumNegatives(input) {
if (input === null || input.length === 0) {
return [];
} else {
console.log(input);
let positives = [];
let negatives = [];
for (let i = 0; i < input.length; i++) {
let currEl = input[i];
currEl > 0 ? positives.push(currEl) : negatives.push(currEl);
}
let positiveCount = positives.length;
let negativeSum = negatives.reduce((sum, num) => sum + num, 0);
return [positiveCount, negativeSum];
}
}
// A couple of small refactors
function countPositivesSumNegatives(input) {
if (!input || input.length === 0) {
return [];
} else {
let positives =[], negatives = [];
for (let i = 0; i < input.length; i++) {
let currEl = input[i];
currEl > 0 ? positives.push(currEl) : negatives.push(currEl);
}
let positiveCount = positives.length;
let negativeSum = negatives.reduce((sum, num) => sum + num, 0);
return [positiveCount, negativeSum];
}
}
countPositivesSumNegatives([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15]); //[10, -65]