Lesson notes
These notes explain the problem and current implementation. They are a reading companion, not a verbatim transcript of the recording.
This lesson replaces six loop patterns with array methods whose names express the intended result. The exercises cover map, filter, includes, find, every, and some. Choosing among them starts with the output contract: a transformed array, a selected array, a matching item, or a boolean answer.
Distinguish transformation from selection
Use map when each visited input element produces a corresponding result. Doubling each number is a transformation. Use filter when a predicate decides which original elements remain. Selecting positive numbers changes membership instead of changing each value. Both methods return new arrays, but their callbacks can still mutate shared objects or external state; allocating a new container does not guarantee deep immutability.
If elements are objects, the selected references remain references to those same objects unless you explicitly copy them. A map that returns an existing object does not create a fresh version of that object. State whether your transformation is intended to preserve or replace identities.
Choose between an item and an existence answer
Find returns the first element whose predicate succeeds, or undefined when none does. Some returns a boolean indicating whether any visited element satisfies its predicate. Every returns whether all visited elements satisfy theirs. Some and every can stop once the answer is determined, while collecting all matches with filter does unnecessary work for an existence-only question.
Includes checks value membership using its equality rule. It is useful when you have the exact value rather than a more general predicate. With objects, equal-looking fields do not establish identity equality. If undefined can itself be a stored element, a find result of undefined alone cannot distinguish a found undefined from absence.
Test empty arrays and callback behavior
For empty input, some is false and every is true. Those results follow from whether any counterexample or successful witness exists. Sparse arrays add method-specific behavior: several callback methods skip holes, while other operations can observe them differently. Follow the input contract rather than assuming every array is dense.
Open the current exercises and predict each output before refactoring. Compare the result, ordering, mutation, and early-exit behavior with the original loop. The JavaScript output quiz offers further practice with language semantics; a concise expression is useful only when it preserves the required behavior.