xxxxxxxxxx
int sumofarr(int *a, int n) {
if (n>1)
return a[n-1]+sumofarr(a, n-1);
return a[0];
}
xxxxxxxxxx
let arr = [2, 4, 1, 3, 5];
let result = 0;
for (let i = 0; i < arr.length; i++) {
result += arr[i];
}
console.log(result); // 15
xxxxxxxxxx
const sum = [1, 2, 3].reduce((partialSum, a) => partialSum + a, 0);
console.log(sum); // 6
Run code snippet
xxxxxxxxxx
const sum = (args) => args.reduce((a, b) => a + b, 0);
console.log(sum(1,2,3)); // returns 6
xxxxxxxxxx
console.log(
[1,2,3].reduce(function(acc, val) { return acc + val; })
)
console.log(
[].reduce(function(acc, val) { return acc + val; }, 0)
)
Run code snippetHide results
xxxxxxxxxx
console.log(
[1,2,3].reduce(function(acc, val) { return acc + val; })
)
console.log(
[].reduce(function(acc, val) { return acc + val; }, 0)
)
Run code snippetHide results
xxxxxxxxxx
console.log(
[1,2,3].reduce(function(acc, val) { return acc + val; })
)
console.log(
[].reduce(function(acc, val) { return acc + val; }, 0)
)
Run code snippetHide results