Skip to content

Commit 214e442

Browse files
committed
Refactor sum function to use Number.isFinite for better number validation
1 parent 8aae3f8 commit 214e442

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

Sprint-1/implement/sum.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ function sum(arr) {
33

44
for (let element of arr) {
55
// iterate through each element in the array
6-
if (typeof element === "number") {
7-
// check if the element is a number
6+
if (Number.isFinite(element)) {
7+
// check if the element is a number, without counting NaN or Infinity
88
total += element; // add the number to the total sum
99
}
1010
}

0 commit comments

Comments
 (0)