Array Sum Javascript
const arr = [1, 2, 3, 4];
const sum = arr.reduce((a, b) => a + b, 0);
// sum = 10
Marton
const arr = [1, 2, 3, 4];
const sum = arr.reduce((a, b) => a + b, 0);
// sum = 10
// codewars solution
// leetcode solution
// and more
function sum(arr) {
return arr.reduce( (x,y) => x+y, 0);
}
sum([1, 2, 3, 4]);
// 10
sum([2, 4, 1, 3, 5]);
// 15
// enjoy :)
const arrSum = arr => arr.reduce((a,b) => a + b, 0)
console.log(
[1, 2, 3, 4].reduce((a, b) => a + b, 0)
)
console.log(
[].reduce((a, b) => a + b, 0)
)
[1, 2, 3, 4].reduce((a, b) => a + b, 0)
// Output: 10
const num = [1, 3, 1, 1]
const reducer = (accumulator, currentValue) => accumulator + currentValue;
const sum = num.reduce(reducer);
console.log(sum)
// (1+3+1+1) = 6