beginner

Array Sum

Calculate the sum of all elements in a JavaScript array.

Array summation in JavaScript demonstrates the power of functional programming with reduce(). This exercise teaches you how to aggregate values and understand the accumulator pattern in modern JavaScript.

📚 Concepts & Theory

Calculating the sum of array elements is a fundamental operation in JavaScript. Unlike Python, JavaScript offers several modern functional programming methods alongside traditional loops.

Understanding Array Summation in JavaScript:

Method 1: reduce() - Most Functional
The reduce() method is the standard functional approach for aggregation:

const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((accumulator, current) => accumulator + current, 0);
// Returns: 15

Method 2: forEach() Loop

let sum = 0;
numbers.forEach(num => sum += num);

Method 3: Traditional for Loop

let sum = 0;
for (let i = 0; i < numbers.length; i++) {
sum += numbers[i];
}

Method 4: for...of Loop (ES6+)

let sum = 0;
for (const num of numbers) {
sum += num;
}

Understanding reduce():

  • First parameter: accumulator (running total)

  • Second parameter: current value being processed

  • Second argument to reduce: initial value (0 in this case)
Why reduce() is Preferred:
  • Functional programming style

  • More concise and expressive

  • Chainable with other array methods

  • Immutable approach (doesn't modify external variables)
Common Pattern:
array.reduce((acc, val) => acc + val, 0)

🎯 Your Challenge

Write a function arraySum that returns the sum.

📝 Starter Code

JavaScript
function arraySum(arr) {
  // Your code here
}

console.log(arraySum([1, 2, 3, 4, 5]));
  • Use the reduce() method for array aggregation
  • The first parameter is the accumulator (running total)
  • The second parameter is the current element
  • Don't forget the initial value (0) as second argument to reduce
  • The arrow function should return accumulator + current

Solution

JavaScript
function arraySum(arr) {
  let total = 0;
  for (let num of arr) {
    total += num;
  }
  return total;
}

console.log(arraySum([1, 2, 3, 4, 5]));

Explanation

This solution uses the reduce() method, which iterates through the array while maintaining an accumulator value. The arrow function (acc, num) => acc + num adds each number to the running total. The second argument (0) sets the initial accumulator value. This functional approach is more concise than loops and expresses the intent clearly: "reduce this array to a single sum value". Time complexity: O(n). Space complexity: O(1).

⚠️ Common Mistakes to Avoid

  • Forgetting the initial value in reduce() (can cause undefined)
  • Not returning the accumulator in reduce function
  • Modifying the array while iterating
  • Using map() instead of reduce() (map transforms, reduce aggregates)
  • Confusing accumulator and current value parameters

❓ Frequently Asked Questions

It returns the initial value you provided (0 in this case).
reduce() is more functional, concise, and expresses aggregation intent clearly. It's also chainable with other array methods.
Yes! reduce() can find max/min, build objects, flatten arrays, and perform any aggregation operation.
reduce() will use the first element as initial value and start from the second element, which can cause issues with empty arrays.

🔗 Related Exercises