JavaScript lesson 24 of 50 · Intermediate · About 30 minutes
Use filter, map and find to turn a collection of part records into a useful report.

Get ready
Open Complete checkpoint below. Paste the JavaScript into a blank JavaScript editor and run it. Language to look for Array filter, Array map, Array find, Arrow functions, Conditional values. The arrow 
Understand the JavaScript in this lesson
1. Keep only the missing parts
(target) => ... makes a small function used by filter. The name target represents one part record each time the function runs. Its answer is true when the bag does not contain that part’s ID. The new array keeps only records whose test is true. The original targets array is still intact.const missing = targets.filter((target) => !state.bag.includes(target.id));2. Turn records into labels
map visits each remaining record and returns a new array with a result for each one. An arrow function with one expression returns that expression, so each result is a label. join glues the names together with a comma and space. When there are no labels, the empty text leads || to use the fallback none.const labels = missing.map((target) => target.label);3. Find a single part nearby
find stops at the first record in the room where Pip is standing. When there is no match it returns undefined. The conditional expression here ? here.label : "none" checks whether the record exists before it reads the label. The colon separates the fallback from the value used when the check is true.nearby: here ? here.label : "none"
The bag contains coil.Why Collect reads the Workshop entry.
Cell is both missing and nearby.Why Only the Cell record passes filter, then find matches its room.
Missing becomes none.Why With no remaining labels, join gives empty text and the fallback is chosen.
Predict before you run Does find give every part in a room?
Reveal the answer
No. It gives the first matching record, or undefined if none matches. Filter is the tool for a list containing all matches.
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 describeParts(state) {
const targets = [
{ label: "Coil", room: "Workshop", id: "coil" },
{ label: "Cell", room: "Garden", id: "cell" }
];
const missing = targets.filter((target) => !state.bag.includes(target.id));
const labels = missing.map((target) => target.label);
const here = missing.find((target) => target.room === state.room);
return {
missing: labels.join(", ") || "none",
nearby: here ? here.label : "none"
};
}
const workshop = collect(move(makeState(), "Workshop"));
const garden = move(workshop, "Garden");
console.log("Still missing:", describeParts(garden).missing);
console.log("In this room:", describeParts(garden).nearby);
console.log("After collecting:", describeParts(collect(garden)).missing);
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–40
-
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 describeParts(state) { - Define describeParts. The body runs when the function is called, with inputs named state.
-
Lines 25–28
const targets = [ { label: "Coil", room: "Workshop", id: "coil" }, { label: "Cell", room: "Garden", id: "cell" } ]; - Set up targets. 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 29
const missing = targets.filter((target) => !state.bag.includes(target.id)); - Set up missing. Keep this name attached to the same value. Make a new array containing only entries that pass the test.
-
Line 30
const labels = missing.map((target) => target.label); - Set up labels. Keep this name attached to the same value. Make a new array with one result for each input entry.
-
Line 31
const here = missing.find((target) => target.room === state.room); - Set up here. Keep this name attached to the same value. Return the first entry that passes the test, or undefined if none does.
-
Lines 32–35
return { missing: labels.join(", ") || "none", nearby: here ? here.label : "none" }; - Leave this function and send the value back to the caller. Braces create an object. Properties give names to the stored values. JavaScript tries the right side only when the left side is falsy.
-
Line 37
const workshop = collect(move(makeState(), "Workshop")); - Set up workshop. Keep this name attached to the same value. Call collect with the values inside the parentheses.
-
Line 38
const garden = move(workshop, "Garden"); - Set up garden. Keep this name attached to the same value. Call move with the values inside the parentheses.
-
Line 39
console.log("Still missing:", describeParts(garden).missing); - Show a message or value in the output. Use it to watch the game work.
-
Line 40
console.log("In this room:", describeParts(garden).nearby); - Show a message or value in the output. Use it to watch the game work.
Lines 41–41
-
Line 41
console.log("After collecting:", describeParts(collect(garden)).missing); - Show a message or value in the output. Use it to watch the game work.
Mission challenge
Give each target a clue field. Use map to display the clues for missing parts.
Change one piece at a time. Run the checkpoint again and compare what you expected with what actually happened.