JavaScript lesson 25 of 50 · Intermediate · About 35 minutes
Put the functions that other code can use behind a small Game object.

Get ready
Open Complete checkpoint below. Paste the JavaScript into a blank JavaScript editor and run it. Language to look for Object property shorthand, Object.freeze, every, Interface. An interface is the set of tools other parts of your program should use. The first guard returns early if Pip is in the wrong room. 
Understand the JavaScript in this lesson
1. Choose the public rules
Game groups the four public rule functions. In { makeState, move }, each name is shorthand for a property with the same name and value. Game.move(...) means look up the function stored at the move property and call it.const Game = Object.freeze({ makeState, move, collect, repair });2. Keep the front door steady
Object.freeze prevents a property of the Game object being replaced, added or removed. It does not freeze the state objects returned by these functions. Grouping the rules gives the future browser controls a consistent set of calls. The screen can choose which rule to call and then display the returned state.const Game = Object.freeze({ makeState, move, collect, repair });3. Make repair obey two checks
every checks that the arrow function is true for each item in the required-parts array. The ! makes the guard fire if any part is missing. When both guards pass, a new state is built with rescued set to true.if (!["coil", "cell"].every((part) => state.bag.includes(part))) return state;
Rescue stays false.Why The room guard stops the call.
The bag has coil and cell.Why Each call returns a state used by the next call.
The new rescue flag is true.Why The room check and the every check both pass.
Predict before you run Does freezing Game freeze the rover’s state?
Reveal the answer
No. Freezing this object protects its own function properties. The route rules still create new state objects.
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 });
let current = Game.makeState();
console.log("Start rescue:", Game.repair(current).rescued);
current = Game.collect(Game.move(current, "Workshop"));
current = Game.collect(Game.move(current, "Garden"));
current = Game.move(current, "Beacon");
current = Game.repair(current);
console.log("Final:", current.room, current.energy, current.rescued);
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
let current = Game.makeState(); - Set up current. Give this name a starting value that later code can change. Call Game.makeState with the values inside the parentheses.
-
Line 31
console.log("Start rescue:", Game.repair(current).rescued); - Show a message or value in the output. Use it to watch the game work.
-
Line 32
current = Game.collect(Game.move(current, "Workshop")); - Call Game.collect with the values inside the parentheses. Store the result in the name or property to the left.
Lines 33–36
-
Line 33
current = Game.collect(Game.move(current, "Garden")); - Call Game.collect with the values inside the parentheses. Store the result in the name or property to the left.
-
Line 34
current = Game.move(current, "Beacon"); - Call Game.move with the values inside the parentheses. Store the result in the name or property to the left.
-
Line 35
current = Game.repair(current); - Call Game.repair with the values inside the parentheses. Store the result in the name or property to the left.
-
Line 36
console.log("Final:", current.room, current.energy, current.rescued); - Show a message or value in the output. Use it to watch the game work.
Mission challenge
Add a Game.report function before freezing the interface. Have it return a short string with the room, energy and bag.
Change one piece at a time. Run the checkpoint again and compare what you expected with what actually happened.