JavaScript lesson 23 of 50 · Intermediate · About 30 minutes
Find a repair part using Pip’s room and put it in a new inventory.

Get ready
Open Complete checkpoint below. Paste the JavaScript into a blank JavaScript editor and run it. Language to look for Property lookup, Undefined, Array spread, Early return. The The first guard returns the current state if there is no part or the bag already contains it. Square brackets start a new array. 
Understand the JavaScript in this lesson
1. Look up a part by room
PARTS object stores a part name under each room that has one. PARTS[state.room] first reads the room name, then looks for it as a key. If the room has no entry, JavaScript gives undefined, its value for a missing result. A single lookup handles both collecting locations.const part = PARTS[state.room];2. Decide whether the part may be taken
! treats a missing part as a reason to stop. includes checks the actual strings in the bag. Check before making any copy so a repeated attempt leaves the game state as it was.if (!part || state.bag.includes(part)) return state;3. Build a new inventory array
...state.bag copies each existing element into it, and the comma lets you add part at the end. The outer object spread carries over all other state values. These two spreads do different jobs. One collects array items and the other copies named properties.return { ...state, bag: [...state.bag, part] };
Pip arrives with an empty bag.Why Moving changes location and energy.
The new bag contains coil.Why Lookup finds the Workshop part and spread copies the array.
No entry is added.Why The bag includes the part, so the guard returns early.
Predict before you run What would collect(makeState()) return?
Reveal the answer
It would return the original starting state, because the Hub is not a key in PARTS and the lookup gives undefined.
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] };
}
const workshop = move(makeState(), "Workshop");
const first = collect(workshop);
const second = collect(first);
console.log("Original bag:", workshop.bag.length);
console.log("Packed:", first.bag.join(", "));
console.log("Repeat kept the same state:", first === second);
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–29
-
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
const workshop = move(makeState(), "Workshop"); - Set up workshop. Keep this name attached to the same value. Call move with the values inside the parentheses.
-
Line 25
const first = collect(workshop); - Set up first. Keep this name attached to the same value. Call collect with the values inside the parentheses.
-
Line 26
const second = collect(first); - Set up second. Keep this name attached to the same value. Call collect with the values inside the parentheses.
-
Line 27
console.log("Original bag:", workshop.bag.length); - Show a message or value in the output. Use it to watch the game work.
-
Line 28
console.log("Packed:", first.bag.join(", ")); - Show a message or value in the output. Use it to watch the game work.
-
Line 29
console.log("Repeat kept the same state:", first === second); - Show a message or value in the output. Use it to watch the game work.
Mission challenge
Move from Workshop to Garden, collect once and check that both parts appear in the new bag.
Change one piece at a time. Run the checkpoint again and compare what you expected with what actually happened.