JS Sum of Array
[1, 2, 3, 4].reduce((a, b) => a + b, 0)
// Output: 10
stdafx-h
[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
function sumGrades(grades) {
let sum =0;
grades.forEach(function(grade){
sum += grade;
})
return sum;
}
// Sample usage - do not modify
console.log(sumGrades([15, 5, 10])); // 30
console.log(sumGrades([12, 10, 13, 19])); // 54