This page is for later. Come back to it after about a week, and again after about a month, without rereading the lessons first. Each challenge mixes two days' ideas in a task you have not seen solved, because recalling a rule in a new combination is what shows you still have it.
Before each challenge, answer from memory: when does a generator's body, including its validation, run? What does this refer to inside a method passed as a callback? When does the pool's outer promise reject, relative to the jobs still running? Why does bounded concurrency still need memory that grows with the input? Then check your answers against the lessons.
Predict, run, and explain
Write the output and trace the important state changes before checking it. Then run the program. Each editor is independent. Async examples end with main() so this editor waits for their work.
const original = { nested: { count: 1 } };
const copy = { ...original };
copy.nested.count = 2;
console.log(original.nested.count);Check your prediction
2Spread copies one level. copy.nested and original.nested are the same object, so changing it through either name changes both. This is the Day 4 ownership rule: a snapshot that should not change needs every shared level copied or frozen.
After a week
After a week: a method as a listener
This mixes Day 1 (this) with Day 4 (listeners and reentrancy). The emitter below stores one record per subscription. Fix two things. First, tally.add is subscribed without its object, so the total stays 0. Second, emit iterates the live Set, so a listener added during an emit runs in that same emit. The policy you want is a snapshot: listeners added during an emit wait for the next one. Do not change Tally or the test loop except for the subscription line. Print:
total: 12
once 5, late 7function createEmitter() {
// First draft: iterates the live Set, so changes during emit leak in.
const listeners = new Set();
return {
on(fn) {
const record = { fn };
listeners.add(record);
return () => listeners.delete(record);
},
emit(value) {
for (const record of listeners) record.fn(value);
}
};
}
class Tally {
total = 0;
add(n) {
this.total += n;
}
}
const tally = new Tally();
const events = createEmitter();
const log = [];
events.on(tally.add); // passes the method without its object
const offOnce = events.on(n => {
log.push(`once ${n}`);
offOnce();
events.on(m => log.push(`late ${m}`));
});
for (const value of [5, 7]) {
try {
events.emit(value);
} catch (error) {
log.push(`${error.name} on ${value}`);
}
}
console.log(`total: ${tally.total}`);
console.log(log.join(", "));Hint 1: the idea
Two different days' rules are at work. A method read off an object does not remember that object: this is decided by how the function is eventually called. And which listeners run during one emit should be decided before the first one is called, because a listener may add or remove subscriptions.
Hint 2: a first step
Run the starter: the total stays 0 and there is no error at all. Look at how emit calls each function, record.fn(value), and work out which object this is during that call. Fix the subscription line first, then run again and look for late 5 in the log: that entry should not be there, and it comes from the emit loop.
Show one solution
function createEmitter() {
const listeners = new Set();
return {
on(fn) {
const record = { fn };
listeners.add(record);
return () => listeners.delete(record);
},
emit(value) {
for (const record of [...listeners]) record.fn(value);
}
};
}
class Tally {
total = 0;
add(n) {
this.total += n;
}
}
const tally = new Tally();
const events = createEmitter();
const log = [];
events.on(tally.add.bind(tally));
const offOnce = events.on(n => {
log.push(`once ${n}`);
offOnce();
events.on(m => log.push(`late ${m}`));
});
for (const value of [5, 7]) {
try {
events.emit(value);
} catch (error) {
log.push(`${error.name} on ${value}`);
}
}
console.log(`total: ${tally.total}`);
console.log(log.join(", "));total: 12
once 5, late 7In the first draft, record.fn(value) called add as a method of the record, so this.total += n wrote NaN onto the emitter's record and never touched tally: a silent bug, not a crash. tally.add.bind(tally) creates a function whose this is always tally, however it is called, which gives total: 12. Copying the Set with [...listeners] before the loop fixes the membership of each emit: the "once" listener removes itself and adds the late one during emit(5), and neither change affects that emit. So late first runs on 7, and once never runs again. Iterating the live Set instead visits entries added during the loop, which is how the first draft also logged late 5.
After a month
After a month: lazy and linear
This mixes Day 2 (generators) with Day 6 (growth). uniqueIncludes is supplied: it removes duplicates by scanning everything kept so far, and counts each comparison. Write uniqueLazy(values, counter), a generator that yields each value the first time it appears, using a Set, and counts one read and one check per value. Predict the counts before you run. Print:
n=1000: same true | includes checks 50400 | Set checks 1000
n=10000: same true | includes checks 5004000 | Set checks 10000
first three: 0,1,2 after 3 readsfunction uniqueIncludes(values, counter) {
const out = [];
for (const value of values) {
counter.reads += 1;
let found = false;
for (const kept of out) {
counter.checks += 1;
if (kept === value) {
found = true;
break;
}
}
if (!found) out.push(value);
}
return out;
}
function takeFirst(count, iterable) {
const out = [];
if (count <= 0) return out;
for (const value of iterable) {
out.push(value);
if (out.length === count) break;
}
return out;
}
function* uniqueLazy(values, counter) {
// Yield each value the first time it appears. Count one read and
// one check per value; use a Set, not a scan of earlier values.
}
for (const size of [1000, 10000]) {
const values = Array.from({ length: size }, (_, i) => (i * 7) % (size / 10));
const slow = { reads: 0, checks: 0 };
const fast = { reads: 0, checks: 0 };
const a = uniqueIncludes(values, slow);
const b = [...uniqueLazy(values, fast)];
console.log(`n=${size}: same ${a.join() === b.join()} | includes checks ${slow.checks} | Set checks ${fast.checks}`);
}
const firstThree = { reads: 0, checks: 0 };
const big = Array.from({ length: 10000 }, (_, i) => i % 100);
console.log(`first three: ${takeFirst(3, uniqueLazy(big, firstThree)).join(",")} after ${firstThree.reads} reads`);Hint 1: the idea
A generator that yields first occurrences needs to remember what it has yielded; a Set does that with one has per value instead of a scan. Because it is a generator, a consumer that stops early also stops the reading.
Hint 2: a first step
Loop over values, add 1 to both counters for each value, and yield it when seen.has(value) is false, adding it to seen first. Compare the two checks numbers at the two sizes before looking at the last line.
Show one solution
function uniqueIncludes(values, counter) {
const out = [];
for (const value of values) {
counter.reads += 1;
let found = false;
for (const kept of out) {
counter.checks += 1;
if (kept === value) {
found = true;
break;
}
}
if (!found) out.push(value);
}
return out;
}
function takeFirst(count, iterable) {
const out = [];
if (count <= 0) return out;
for (const value of iterable) {
out.push(value);
if (out.length === count) break;
}
return out;
}
function* uniqueLazy(values, counter) {
const seen = new Set();
for (const value of values) {
counter.reads += 1;
counter.checks += 1;
if (!seen.has(value)) {
seen.add(value);
yield value;
}
}
}
for (const size of [1000, 10000]) {
const values = Array.from({ length: size }, (_, i) => (i * 7) % (size / 10));
const slow = { reads: 0, checks: 0 };
const fast = { reads: 0, checks: 0 };
const a = uniqueIncludes(values, slow);
const b = [...uniqueLazy(values, fast)];
console.log(`n=${size}: same ${a.join() === b.join()} | includes checks ${slow.checks} | Set checks ${fast.checks}`);
}
const firstThree = { reads: 0, checks: 0 };
const big = Array.from({ length: 10000 }, (_, i) => i % 100);
console.log(`first three: ${takeFirst(3, uniqueLazy(big, firstThree)).join(",")} after ${firstThree.reads} reads`);n=1000: same true | includes checks 50400 | Set checks 1000
n=10000: same true | includes checks 5004000 | Set checks 10000
first three: 0,1,2 after 3 readsBoth versions keep the same values in the same order. The includes-style scan checks each value against everything kept so far, so with n/10 distinct values its comparisons grow about 100-fold when n grows 10-fold: O(n²) once the number of distinct values grows with n. The Set version does exactly one check per value, so its count grows 10-fold: O(n). The last line is the Day 2 half. takeFirst breaks after three values, the break closes the generator, and only 3 of the 10,000 values are ever read. The eager version would have read and compared all of them first.
Change it and test it
Rebuild the On Your Own retry brief from memory, using controlled failures instead of timers, and explain the callback assumptions before coding. If you want types to enforce more caller relationships, continue with TypeScript beginner and intermediate; no framework is required for either path.
Ready for the next step?
Before starting another course, build a smaller library in a new domain and write down its ownership, ordering, failure, and cleanup rules. Prove one deliberate implementation defect is caught by your tests. If static types are new to you, TypeScript beginner starts from familiar JavaScript; follow its levels in order rather than jumping directly to type-level programming. You can continue practicing JavaScript library design without adopting TypeScript.
Completion is saved in this browser only. It does not measure mastery.