Easy javascript

Sum of array elements

Problem

Given [3, 7, 2, 9, 1], print the sum using reduce or a loop.

Hints
  • reduce((acc, val) => acc + val, 0)

Your practice code

Ready — edit the code above and click Run.

Solution

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.

Explanation

reduce is the functional approach; for...of works too.

Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details