Ready. Start with one instruction.
Loading the learning workspace…
FROM YOUR FIRST INSTRUCTION TO A WORKING APPLICATION
Learn JavaScript from scratch through small working examples, visual experiments and complete projects. No account or AI key required.
Predict → Experiment → Build → Test → Explain
Ready. Start with one instruction.
Loading the learning workspace…
reduce carries an accumulator through a collection. The initial value here is zero. Each callback receives the earlier sum and current number; its return becomes the next accumulator. The empty-array case returns the supplied initial value without running the callback. Compare this with a loop that updates sum manually. Omitting the initial value changes the empty-array behavior, and returning only number discards earlier contributions.
function solve(numbers) {
return numbers.reduce((sum, number) => sum + number, 0);
}
console.log(solve([2,3,4]));9
reduce carries an accumulator through a collection. The initial value here is zero. Each callback receives the earlier sum and current number; its return becomes the next accumulator. The empty-array case returns the supplied initial value without running the callback. Compare this with a loop that updates sum manually. Omitting the initial value changes the empty-array behavior, and returning only number discards earlier contributions.
function solve(numbers) {This statement belongs to the operation described above. Compare its names, arguments and current state before advancing.
return numbers.reduce((sum, number) => sum + number, 0);Return the value of this exact expression to the caller. Follow its inputs using the concept explanation above.
}Close the current instruction block.
console.log(solve([2,3,4]));Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
function solve(numbers) {
return numbers.reduce((sum, number) => sum + number, 0);
}
console.log(solve([5]));5
reduce carries an accumulator through a collection. The initial value here is zero. Each callback receives the earlier sum and current number; its return becomes the next accumulator. The empty-array case returns the supplied initial value without running the callback. Compare this with a loop that updates sum manually. Omitting the initial value changes the empty-array behavior, and returning only number discards earlier contributions.
function solve(numbers) {This statement belongs to the operation described above. Compare its names, arguments and current state before advancing.
return numbers.reduce((sum, number) => sum + number, 0);Return the value of this exact expression to the caller. Follow its inputs using the concept explanation above.
}Close the current instruction block.
console.log(solve([5]));Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
function solve(numbers) {
return numbers.reduce((sum, number) => sum + number, 0);
}
console.log(solve([]));0
reduce carries an accumulator through a collection. The initial value here is zero. Each callback receives the earlier sum and current number; its return becomes the next accumulator. The empty-array case returns the supplied initial value without running the callback. Compare this with a loop that updates sum manually. Omitting the initial value changes the empty-array behavior, and returning only number discards earlier contributions.
function solve(numbers) {This statement belongs to the operation described above. Compare its names, arguments and current state before advancing.
return numbers.reduce((sum, number) => sum + number, 0);Return the value of this exact expression to the caller. Follow its inputs using the concept explanation above.
}Close the current instruction block.
console.log(solve([]));Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
Repair the intentionally changed operation so the example produces the exact expected result. Then try the extension without copying the answer.
function solve(numbers) {
return numbers.reduce((sum, number) => number, 0);
}
console.log(solve([2,3,4]));function solve(numbers) {
return numbers.reduce((sum, number) => sum + number, 0);
}
console.log(solve([2,3,4]));Sum all numbers, including negative values.
Which operation changed the result, why did it work, and what would you test next?
Use a different value that could expose a boundary or mistake. Predict the result first, run it, and explain how the concept would help in a small real application.
Related: Previous lesson Next lesson
100 lessons across 20 modules, covering variables, operators, strings, conditions, loops, functions, scope, arrays, objects, DOM, events, state, testing, asynchronous JavaScript and application development.