Skip to content
Chapter 3Lesson 9

Quiz - Picking the right container

Quiz progress

0 / 0

A click handler runs this code and the React list never re-orders on screen, even though the underlying array is genuinely sorted:

setInvoices(invoices.sort((a, b) => a.amountCents - b.amountCents));

What is the senior one-line fix?

Replace .sort with .toSorted so a new array reference is handed to setInvoices.
Wrap the call in setInvoices([...invoices].sort(...)) to force React to re-render.
Call setInvoices(invoices) a second time so React picks up the in-place mutation.

Under noUncheckedIndexedAccess, you read a field whose key comes from a fieldName variable. Which form says what the code means and gets the right type?

obj.fieldName — the dot is the default reach for any field access.
obj[fieldName] — bracket access is earned by the dynamic-key trigger, and the result is correctly typed as T | undefined.
obj['fieldName'] — quoting the variable name keeps the dot semantics with a string key.

Which .filter call narrows (string | null)[] to string[]?

rawIds.filter(Boolean)
rawIds.filter((x) => x !== null)
rawIds.filter((x) => !!x)

A 10,000-row invoice list is filtered with bigInvoices.filter((i) => watchedIds.includes(i.customerId)) where watchedIds is a 200-element array. The senior refactor is:

Build a Set from watchedIds once outside the filter, then .has inside — turning O(n × m) into O(n + m).
Replace .filter with a for...of loop so the inner check short-circuits.
Sort both arrays first so .includes can binary-search the watch list.

Predict the output. Given:

function* events() {
for (const item of ['a', 'b', 'c', 'd', 'e']) {
console.log(`yielding ${item}`);
yield item;
}
}
const result = Iterator.from(events())
.filter((x) => x === 'b' || x === 'd' || x === 'e')
.take(2)
.toArray();

How many yielding lines print before result is assigned?

2 — .take(2) limits the source to two yields.
4 — the source is pulled until .take(2) has accepted its second value (b then d).
5 — the entire source is materialized before the chain runs.

A multi-select UI holds the selected invoice IDs in a Set and POSTs them to the server:

await fetch('/api/save', {
method: 'POST',
body: JSON.stringify({ selected }),
});

What does the server receive in selected?

The full array of IDs — JSON.stringify serializes Set entries as an array.
An empty object — JSON.stringify only sees own enumerable properties, and Set stores entries in internal slots.
A string like "Set(2) { 'inv_1', 'inv_2' }" — the default toString representation.

A sign-up form validates usernames with /^[a-zA-Z0-9]+$/u.test(input) and rejects Müller, José, and 小明. What’s the senior fix?

Add the i flag so the regex is case-insensitive across scripts.
Replace [a-zA-Z] with the \p{Letter} property escape (the u flag is what makes it work).
Switch to the v flag so emoji and accented characters match.

You want every match of a capturing regex on a string, with access to each match’s .groups. Which method do you reach for?

string.match(pattern) with the g flag.
string.matchAll(pattern) with the g flag.
pattern.test(string) in a loop with lastIndex.

Which scenario triggers reaching for for...of over a method chain?

You need to await an async call per item, sequenced one after another.
The source array has more than 1000 elements.
You want to combine .filter and .map in one walk.

You run node script.ts on Node 24 and get Cannot find package '@/lib'. What’s the right reach?

Pass --experimental-resolve-paths to node so it reads tsconfig.json.
Switch to tsx — it reads tsconfig.json and resolves the path alias.
Run tsc --noEmit first to compile the alias into a relative import.

Which of these belong in the repo’s .vscode/settings.json rather than in your personal User settings? Select all that apply.

editor.formatOnSave: true
editor.defaultFormatter: "biomejs.biome"
workbench.colorTheme: "GitHub Dark"
editor.fontFamily: "JetBrains Mono"

Quiz complete

Score by topic