programming-examples/js/Math/Calculate the sum of values in an array.js
2019-11-15 12:59:38 +01:00

18 lines
540 B
JavaScript

function sum(input){
if (toString.call(input) !== "[object Array]")
return false;
var total = 0;
for(var i=0;i<input.length;i++)
{
if(isNaN(input[i])){
continue;
}
total += Number(input[i]);
}
return total;
}
console.log(sum([1,2,3]));
console.log(sum([100,-200,3]));
console.log(sum([1,2,'a',3]));