Given [3, 7, 2, 9, 1], print the sum using reduce or a loop.
Ready — edit the code above and click Run.
const arr = [3, 7, 2, 9, 1];
const sum = arr.reduce((a, b) => a + b, 0);
console.log(sum);
Try solving on your own first, then reveal the official answer.
reduce is the functional approach; for...of works too.