JavaScript lesson 29 of 50 · Intermediate · About 40 minutes
Turn a seed and a list of actions into a rescue you can repeat exactly.

Get ready
Open Complete checkpoint below. Paste the JavaScript into a blank JavaScript editor and run it. Language to look for Seeded input, Remainder operator, reduce, Deterministic replay. A seed is an input number used to make a repeatable result. This tiny supply rule multiplies the seed by 17, adds 11, then uses The flight array stores action objects in order. It records what Pip tried, including a separate destination for move actions. Each action has the same shape that 
Understand the JavaScript in this lesson
1. Use a seed to choose a starting supply
% to take the remainder when dividing by 3. The answer is 0, 1 or 2. Three plus that amount sets Pip’s starting energy. This is a simple supply pattern for the game. It is not a security or scientific random generator.const supplies = (seed * 17 + 11) % 3;2. Record actions as data
apply already understands, which means live play and replay can use the same dispatcher. To replay a different route, change the list of actions without changing the engine.{ type: "move", destination: "Workshop" },3. Let each result feed the next action
reduce works through an array, passing a running result to the function each time. Here the running result is the current game state. launch(seed) supplies the first one. The arrow function returns what apply gives back. The next action receives that state. With the same seed and action list, this rule produces the same final result.return actions.reduce((state, action) => apply(state, action), launch(seed));
Energy starts at five.Why Six times 17 plus 11 gives 113, with remainder 2.
Pip collects two parts and travels three times.Why The state from each action becomes the input to the next.
Both saved result texts match.Why The starting supply and ordered actions are the same.
Predict before you run What happens if you leave the final repair action out?
Reveal the answer
Pip arrives at Beacon with both parts but the rescue flag remains false. Only a successful repair sets it to true.
Complete checkpoint
JavaScript · the instructions
const MAP = {
Hub: ["Workshop", "Garden"],
Workshop: ["Hub", "Garden"],
Garden: ["Hub", "Workshop", "Beacon"],
Beacon: ["Garden"]
};
const PARTS = { Workshop: "coil", Garden: "cell" };
function makeState() {
return { room: "Hub", energy: 5, bag: [], rescued: false };
}
function move(state, destination) {
const { room, energy } = state;
if (!MAP[room].includes(destination) || energy < 1) {
return state;
}
return { ...state, room: destination, energy: energy - 1 };
}
function collect(state) {
const part = PARTS[state.room];
if (!part || state.bag.includes(part)) return state;
return { ...state, bag: [...state.bag, part] };
}
function repair(state) {
if (state.room !== "Beacon") return state;
if (!["coil", "cell"].every((part) => state.bag.includes(part))) return state;
return { ...state, rescued: true };
}
const Game = Object.freeze({ makeState, move, collect, repair });
function apply(state, action) {
switch (action.type) {
case "move": return Game.move(state, action.destination);
case "collect": return Game.collect(state);
case "repair": return Game.repair(state);
default: throw new Error("Unknown action: " + action.type);
}
}
function launch(seed) {
if (!Number.isInteger(seed) || seed < 0 || seed > 99) {
throw new Error("Choose a whole-number seed from 0 to 99");
}
const supplies = (seed * 17 + 11) % 3;
return { ...Game.makeState(), energy: 3 + supplies };
}
function replay(seed, actions) {
return actions.reduce((state, action) => apply(state, action), launch(seed));
}
const flight = [
{ type: "move", destination: "Workshop" },
{ type: "collect" },
{ type: "move", destination: "Garden" },
{ type: "collect" },
{ type: "move", destination: "Beacon" },
{ type: "repair" }
];
const one = replay(6, flight);
const two = replay(6, flight);
console.log("Starting supply:", launch(6).energy);
console.log("Replay:", one.room, one.energy, one.rescued);
console.log("Repeat result:", JSON.stringify(one) === JSON.stringify(two));
Read the JavaScript line by line
Open a small group of lines while you read the complete checkpoint. Blank lines separate ideas. Closing braces mark the end of a block. An ellipsis here means you can read the rest in the full program.
Lines 1–21
-
Lines 1–6
const MAP = { Hub: ["Workshop", "Garden"], Workshop: ["Hub", "Garden"], Garden: ["Hub", "Workshop", "Beacon"], Beacon: ["Garden"] }; - Set up MAP. Keep this name attached to the same value. Braces create an object. Properties give names to the stored values. Square brackets create an ordered array. Its first position is 0.
-
Line 7
const PARTS = { Workshop: "coil", Garden: "cell" }; - Set up PARTS. Keep this name attached to the same value. Braces create an object. Properties give names to the stored values.
-
Line 9
function makeState() { - Define makeState. The body runs when the function is called.
-
Line 10
return { room: "Hub", energy: 5, bag: [], rescued: false }; - Leave this function and send the value back to the caller. Braces create an object. Properties give names to the stored values. Square brackets create an ordered array. Its first position is 0.
-
Line 12
function move(state, destination) { - Define move. The body runs when the function is called, with inputs named state, destination.
-
Line 13
const { room, energy } = state; - Set up { room, energy }. Keep this name attached to the same value.
-
Line 14
if (!MAP[room].includes(destination) || energy < 1) { - Run this part when the test is truthy. JavaScript tries the right side only when the left side is falsy. The exclamation mark reverses a true-or-false test.
-
Line 15
return state; - Leave this function and send the value back to the caller.
-
Line 17
return { ...state, room: destination, energy: energy - 1 }; - Leave this function and send the value back to the caller. Braces create an object. Properties give names to the stored values. Three dots unpack the existing entries here.
-
Line 19
function collect(state) { - Define collect. The body runs when the function is called, with inputs named state.
-
Line 20
const part = PARTS[state.room]; - Set up part. Keep this name attached to the same value. Work out what is inside the brackets first, then use it as the key or index.
-
Line 21
if (!part || state.bag.includes(part)) - Run this part when the test is truthy. JavaScript tries the right side only when the left side is falsy. The exclamation mark reverses a true-or-false test.
Lines 21–32
-
Line 21
return state; - Leave this function and send the value back to the caller.
-
Line 22
return { ...state, bag: [...state.bag, part] }; - Leave this function and send the value back to the caller. Braces create an object. Properties give names to the stored values. Three dots unpack the existing entries here.
-
Line 24
function repair(state) { - Define repair. The body runs when the function is called, with inputs named state.
-
Line 25
if (state.room !== "Beacon") - Run this part when the test is truthy. The exclamation and two equals signs check whether values differ without type conversion.
-
Line 25
return state; - Leave this function and send the value back to the caller.
-
Line 26
if (!["coil", "cell"].every((part) => state.bag.includes(part))) - Run this part when the test is truthy. The exclamation mark reverses a true-or-false test. Square brackets create an ordered array. Its first position is 0.
-
Line 26
return state; - Leave this function and send the value back to the caller.
-
Line 27
return { ...state, rescued: true }; - Leave this function and send the value back to the caller. Braces create an object. Properties give names to the stored values. Three dots unpack the existing entries here.
-
Line 29
const Game = Object.freeze({ makeState, move, collect, repair }); - Set up Game. Keep this name attached to the same value. Prevent direct edits to the properties of this object.
-
Line 30
function apply(state, action) { - Define apply. The body runs when the function is called, with inputs named state, action.
-
Lines 31–32
switch (action.type) { - Compare one value with the case labels, then follow the matching branch.
-
Line 32
case "move": - Run this branch when its label matches.
Lines 32–43
-
Line 32
return Game.move(state, action.destination); - Leave this function and send the value back to the caller. Call Game.move with the values inside the parentheses.
-
Line 33
case "collect": - Run this branch when its label matches.
-
Line 33
return Game.collect(state); - Leave this function and send the value back to the caller. Call Game.collect with the values inside the parentheses.
-
Line 34
case "repair": - Run this branch when its label matches.
-
Line 34
return Game.repair(state); - Leave this function and send the value back to the caller. Call Game.repair with the values inside the parentheses.
-
Line 35
default: - Use this branch when no label matches.
-
Line 35
throw new Error("Unknown action: " + action.type); - Stop this part of the program and report the problem to the caller or a matching catch block.
-
Line 38
function launch(seed) { - Define launch. The body runs when the function is called, with inputs named seed.
-
Line 39
if (!Number.isInteger(seed) || seed < 0 || seed > 99) { - Run this part when the test is truthy. JavaScript tries the right side only when the left side is falsy. The exclamation mark reverses a true-or-false test.
-
Line 40
throw new Error("Choose a whole-number seed from 0 to 99"); - Stop this part of the program and report the problem to the caller or a matching catch block.
-
Line 42
const supplies = (seed * 17 + 11) % 3; - Set up supplies. Keep this name attached to the same value. The percent sign gives the remainder after division. Plus adds numbers or joins text, depending on its values.
-
Line 43
return { ...Game.makeState(), energy: 3 + supplies }; - Leave this function and send the value back to the caller. Braces create an object. Properties give names to the stored values. Three dots unpack the existing entries here.
Lines 46–61
-
Line 46
function replay(seed, actions) { - Define replay. The body runs when the function is called, with inputs named seed, actions.
-
Line 47
return actions.reduce((state, action) => apply(state, action), launch(seed)); - Leave this function and send the value back to the caller. Combine the entries into a single result, one entry at a time.
-
Lines 49–56
const flight = [ { type: "move", destination: "Workshop" }, { type: "collect" }, { type: "move", destination: "Garden" }, { type: "collect" }, … - Set up flight. Keep this name attached to the same value. Square brackets create an ordered array. Its first position is 0. Braces create an object. Properties give names to the stored values.
-
Line 57
const one = replay(6, flight); - Set up one. Keep this name attached to the same value. Call replay with the values inside the parentheses.
-
Line 58
const two = replay(6, flight); - Set up two. Keep this name attached to the same value. Call replay with the values inside the parentheses.
-
Line 59
console.log("Starting supply:", launch(6).energy); - Show a message or value in the output. Use it to watch the game work.
-
Line 60
console.log("Replay:", one.room, one.energy, one.rescued); - Show a message or value in the output. Use it to watch the game work.
-
Line 61
console.log("Repeat result:", JSON.stringify(one) === JSON.stringify(two)); - Show a message or value in the output. Use it to watch the game work.
Mission challenge
Run flight with seeds 0, 1 and 2. Record each starting and finishing energy and explain the pattern.
Change one piece at a time. Run the checkpoint again and compare what you expected with what actually happened.