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…
A reducer receives state and an action and returns the next state. This centralizes update rules and makes transitions easy to test. An unknown action should usually preserve the state instead of resetting it unexpectedly. An action describes a user’s intent while a reducer computes the next state. Plan data and interface effects separately so changes can be tested.
function solve(input) {
if(input.action==="increment") return input.state+1;
if(input.action==="decrement") return input.state-1;
return input.state;
}
console.log(solve({"state":2,"action":"increment"}));3
The returned value follows the operation in the helper. Compare each element or field, including the boundary case.
function solve(input) {Define a reusable function. Names inside its parentheses are parameters; actual arguments arrive when it is called. Defining the function does not run it.
if(input.action==="increment") return input.state+1;Execute this statement at this point in the program. The update changes the current binding or object; compare state before and after.
if(input.action==="decrement") return input.state-1;Execute this statement at this point in the program. The update changes the current binding or object; compare state before and after.
return input.state;Evaluate the expression after return, end this function call, and give that value to the caller. The result is not automatically displayed.
}Close the current instruction block.
console.log(solve({"state":2,"action":"increment"}));Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
function solve(input) {
if(input.action==="increment") return input.state+1;
if(input.action==="decrement") return input.state-1;
return input.state;
}
console.log(solve({"state":2,"action":"unknown"}));2
The returned value follows the operation in the helper. Compare each element or field, including the boundary case.
function solve(input) {Define a reusable function. Names inside its parentheses are parameters; actual arguments arrive when it is called. Defining the function does not run it.
if(input.action==="increment") return input.state+1;Execute this statement at this point in the program. The update changes the current binding or object; compare state before and after.
if(input.action==="decrement") return input.state-1;Execute this statement at this point in the program. The update changes the current binding or object; compare state before and after.
return input.state;Evaluate the expression after return, end this function call, and give that value to the caller. The result is not automatically displayed.
}Close the current instruction block.
console.log(solve({"state":2,"action":"unknown"}));Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
function solve(input) {
if(input.action==="increment") return input.state+1;
if(input.action==="decrement") return input.state-1;
return input.state;
}
console.log(solve({"state":0,"action":""}));0
The returned value follows the operation in the helper. Compare each element or field, including the boundary case.
function solve(input) {Define a reusable function. Names inside its parentheses are parameters; actual arguments arrive when it is called. Defining the function does not run it.
if(input.action==="increment") return input.state+1;Execute this statement at this point in the program. The update changes the current binding or object; compare state before and after.
if(input.action==="decrement") return input.state-1;Execute this statement at this point in the program. The update changes the current binding or object; compare state before and after.
return input.state;Evaluate the expression after return, end this function call, and give that value to the caller. The result is not automatically displayed.
}Close the current instruction block.
console.log(solve({"state":0,"action":""}));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(input) {
if(input.action==="increment") return undefined; // input.state+1;
if(input.action==="decrement") return input.state-1;
return input.state;
}
console.log(solve({"state":2,"action":"increment"}));function solve(input) {
if(input.action==="increment") return input.state+1;
if(input.action==="decrement") return input.state-1;
return input.state;
}
console.log(solve({"state":2,"action":"increment"}));Handle unknown actions without changing state.
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.