Skip to content

Commit c60f88b

Browse files
author
Ogbemi mene
committed
my sum solution
1 parent 529da6a commit c60f88b

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

‎Sprint-1/implement/sum.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
function sum(elements) {
2+
if (!Array.isArray(elements) || elements.length === 0) {
3+
return 0;
4+
}
5+
6+
const numbersOnly = elements.filter(item => typeof item === "number" && !isNaN(item));
7+
8+
if (numbersOnly.length === 0) {
9+
return 0;
10+
}
11+
12+
return numbersOnly.reduce((acc, curr) => acc + curr, 0);
13+
214
}
315

416
module.exports = sum;

0 commit comments

Comments
 (0)