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…
An array pipeline gives each transformation one responsibility. filter keeps positive numbers, map doubles those selected values, and reduce adds them using zero as the initial accumulator. Each stage consumes the previous stage’s result. Read the stages in that order; mapping or filtering a different source collection would change the contract. An empty input stays empty until reduce returns its initial zero. None of these three calls mutates the source array.
function solve(numbers) {
return numbers.filter(number => number > 0)
.map(number => number * 2)
.reduce((sum, number) => sum + number, 0);
}
console.log(solve([-1,2,3]));10
An array pipeline gives each transformation one responsibility. filter keeps positive numbers, map doubles those selected values, and reduce adds them using zero as the initial accumulator. Each stage consumes the previous stage’s result. Read the stages in that order; mapping or filtering a different source collection would change the contract. An empty input stays empty until reduce returns its initial zero. None of these three calls mutates the source array.
function solve(numbers) {This statement belongs to the operation described above. Compare its names, arguments and current state before advancing.
return numbers.filter(number => number > 0)Return the value of this exact expression to the caller. Follow its inputs using the concept explanation above.
.map(number => number * 2)This statement belongs to the operation described above. Compare its names, arguments and current state before advancing.
.reduce((sum, number) => sum + number, 0);This statement belongs to the operation described above. Compare its names, arguments and current state before advancing.
}Close the current instruction block.
console.log(solve([-1,2,3]));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.filter(number => number > 0)
.map(number => number * 2)
.reduce((sum, number) => sum + number, 0);
}
console.log(solve([0,4]));8
An array pipeline gives each transformation one responsibility. filter keeps positive numbers, map doubles those selected values, and reduce adds them using zero as the initial accumulator. Each stage consumes the previous stage’s result. Read the stages in that order; mapping or filtering a different source collection would change the contract. An empty input stays empty until reduce returns its initial zero. None of these three calls mutates the source array.
function solve(numbers) {This statement belongs to the operation described above. Compare its names, arguments and current state before advancing.
return numbers.filter(number => number > 0)Return the value of this exact expression to the caller. Follow its inputs using the concept explanation above.
.map(number => number * 2)This statement belongs to the operation described above. Compare its names, arguments and current state before advancing.
.reduce((sum, number) => sum + number, 0);This statement belongs to the operation described above. Compare its names, arguments and current state before advancing.
}Close the current instruction block.
console.log(solve([0,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.filter(number => number > 0)
.map(number => number * 2)
.reduce((sum, number) => sum + number, 0);
}
console.log(solve([]));0
An array pipeline gives each transformation one responsibility. filter keeps positive numbers, map doubles those selected values, and reduce adds them using zero as the initial accumulator. Each stage consumes the previous stage’s result. Read the stages in that order; mapping or filtering a different source collection would change the contract. An empty input stays empty until reduce returns its initial zero. None of these three calls mutates the source array.
function solve(numbers) {This statement belongs to the operation described above. Compare its names, arguments and current state before advancing.
return numbers.filter(number => number > 0)Return the value of this exact expression to the caller. Follow its inputs using the concept explanation above.
.map(number => number * 2)This statement belongs to the operation described above. Compare its names, arguments and current state before advancing.
.reduce((sum, number) => sum + number, 0);This statement belongs to the operation described above. Compare its names, arguments and current state before advancing.
}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.filter(number => number < 0)
.map(number => number * 2)
.reduce((sum, number) => sum + number, 0);
}
console.log(solve([-1,2,3]));function solve(numbers) {
return numbers.filter(number => number > 0)
.map(number => number * 2)
.reduce((sum, number) => sum + number, 0);
}
console.log(solve([-1,2,3]));Trim then uppercase the text.
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.