-
-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathsum.js
More file actions
21 lines (16 loc) · 813 Bytes
/
sum.js
File metadata and controls
21 lines (16 loc) · 813 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function sum(elements) {
// adds numbers and ignores non-numbers unless there are no valid numbers then throws error
if (!Array.isArray(elements)) {
throw new Error("Input must be an array"); //checks input is an array if not throws error
}
if (elements.length === 0) return 0; // <-- empty array returns 0
let total = 0;
let foundValid = false; //starts at false and will become true if valid number found
for (const val of elements) {
if (!Number.isFinite(val)) continue;
foundValid = true; // loops and checks if input valid and if valid changes foundValid to true
total += val; //adds number to sum
}
return foundValid ? total : "Invalid Input"; // if at least one valid value is found return sum if no valid number is found return Invalid Input
}
module.exports = sum;