-
-
Notifications
You must be signed in to change notification settings - Fork 279
Expand file tree
/
Copy pathsum.js
More file actions
23 lines (22 loc) · 554 Bytes
/
sum.js
File metadata and controls
23 lines (22 loc) · 554 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function sum(elements) {
// Check if input is an array
if (!Array.isArray(elements)) {
return null;
}
// Check if array is empty
if (elements.length === 0) {
return 0;
}
// Filter only numeric values
const numericElements = elements.filter((item) => typeof item === "number");
if (numericElements.length === 0) {
return -Infinity;
}
// Calculate the sum of numeric values
let total = 0;
for (let i = 0; i < numericElements.length; i++) {
total += numericElements[i];
}
return total;
}
module.exports = sum;