-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountposSumNeg.js
More file actions
32 lines (26 loc) · 937 Bytes
/
CountposSumNeg.js
File metadata and controls
32 lines (26 loc) · 937 Bytes
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
/*Instructions/Task: Given an array of integers.
Return an array, where the first element is the count of positives numbers and the second element is sum of negative numbers. 0 is neither positive nor negative.
If the input is an empty array or is null, return an empty array.
Example
For input [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15], you should return [10, -65].*/
//My Answer
function countPositivesSumNegatives(input) {
if (input === null || input.length === 0) {
return [];
}
let countPositives = 0;
let sumNegatives = 0;
for (let num of input) {
// num = arr[i];
if (num > 0) {
countPositives++;
} else if (num < 0) {
sumNegatives += num;
}
}
return [countPositives, sumNegatives];
}
// // Example usage:
const inputArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15];
const result = countPositivesSumNegatives(inputArray);
console.log(result);