Skip to main content
🟨 JavaScript Foundations

Arrays & Loops

An array is an ordered list. A loop lets you visit each item.

const nums = [1, 2, 3];
let total = 0;
for (const n of nums) {
  total += n;
}
// total is 6

Your task

Write a function sum that takes an array of numbers and returns their total. An empty array should return 0.

⚑ 30 XP

🧱 Code blocks

Drag a block into the editor, or tap it to drop it in where your cursor is. Add them in order to build your answer.

solution.jsRuns in your browser
Loading editor…
Tests3 to pass
sum([1,2,3]) === 6
sum([]) === 0
sum([10, -4, 4]) === 10

Run your code to check these.