$3.5

How to implement a function which will return the sum of array's values?

Ask a question+
2

I have an array with number values [1, 2, 3, 4], how to calculate the sum of its elements?

I thought $.each might be useful, but I’m not sure how to implement it.
The answer can be in JavaScript only too.

Thanks in advance.

BabkenM 970
add comment

2 Answers

1
var arr=new Array(1,2,3,4);
var sum=0;

arr.forEach(function(val){
        sum+=val;
});
console.log(sum);
Arman 250
add comment
1

const arr = [15,2,8,10];

let sum = arr.reduce((currentSum, el) => {
   return currentSum + el;
});
console.log(sum);
add comment

Your Answer