Given an array [1, 2, 3, 4], how can I find the sum of its elements? (In this case, the sum would be 10.)
[1, 2, 3, 4]
10
I thought $.each might be useful,
This is much easier
function sumArray(arr) { var total = 0; arr.forEach(function(element){ total += element; }) return total; } var sum = sumArray([1,2,3,4]) console.log(sum)