Find one, test many
find returns the first matching item or undefined. some asks whether any item matches and returns a boolean. every asks whether all items match. Choose the operation whose result matches the question; do not build a whole filtered array merely to find its first item.
const sessions = [
{ topic: "Loops", minutes: 30 },
{ topic: "Arrays", minutes: 15 }
];
const match = sessions.find(item => item.minutes < 20);
console.log(match.topic);
console.log(sessions.some(item => item.minutes > 60));
console.log(sessions.every(item => item.minutes > 0));Check your prediction
Arrays
false
trueThe example guarantees a match. In general check for undefined before reading match.topic.
If find returns undefined, reading a property from it causes an error. Guard the result first: if (match !== undefined) { console.log(match.topic); }. Try searching for a duration below 1 and use that guard to avoid reading a nonexistent record.
Method calls can be chained because each result becomes the next input: items.filter(rule).map(format) means filter first, then call map on the resulting array. Separate variables are equally valid and often easier to inspect.
Sorting has two surprises
Default sort compares values as strings, so numeric ordering needs a comparator. A comparator returns a negative number to put the first value earlier, zero for a tie, and a positive number to put it later. (a, b) => a - b sorts numbers ascending. This callback has two parameters, so parentheses around them are required. The sorting method supplies pairs of numbers to compare; you do not write the sequence of comparisons yourself. sort also mutates the array; copy with spread first when the original order matters.
For records use (a, b) => a.minutes - b.minutes. This reorders references but does not clone the records. Avoid returning true or false from a comparator: those values do not express all three outcomes.
Experiment: sort [3, 20, 100] with and without a comparator, then inspect the original array. Explain both differences. On an empty array, some returns false and every returns true: there is no counterexample to “all items satisfy the rule”. Require a positive length separately if your application needs at least one item.
Handle no match and preserve the original order
Find returns one matching value, or undefined when none matches. Some answers whether at least one matches; every asks whether all match. These methods are not interchangeable: a boolean cannot tell you which record to display. Decide what form the caller needs before selecting a method.
Sorting has another ownership decision: sort changes its array. A copied array protects the original order, but still shares its record objects. For numbers, provide a comparator; the default compares text forms and can put 100 before 20.
const sessions = [{ topic: "Loops", minutes: 25 }];
const found = sessions.find(item => item.minutes > 60);
if (found === undefined) {
console.log("No long session");
} else {
console.log(found.topic);
}
console.log(sessions.some(item => item.minutes > 60));Check your prediction
No long session
false Read found.topic only after proving a record exists. The empty-result path is normal behavior, not necessarily an exception. Test a matching value too so the guard does not accidentally reject everything.
const minutes = [100, 20, 3];
console.log([...minutes].sort().join(","));
console.log([...minutes].sort((a, b) => a - b).join(","));
console.log(minutes.join(","));Check your prediction
100,20,3
3,20,100
100,20,3 A negative comparator result puts a before b; positive puts it after. The spread protects the input order in both sorts. No change to the original is evidence of the copy, not a default guarantee from sort.
Try the rule on new inputs
Test an empty list, one match and two matches. Explain why find returns only the first match. Sort a copy descending by reversing the comparator subtraction, and verify the original stays in its initial order.
Shortest first
Create a sorted copy of the durations. Print the ordered values and the original values. You do not need to define a function for this exercise.
const durations = [30, 5, 100];
// Preserve the original order.Hint 1: the idea
Copy first, then sort with a numeric comparator.
Hint 2: a first step
Subtract the second callback parameter from the first for ascending order.
Show one solution
const durations = [30, 5, 100];
const ordered = [...durations].sort((a, b) => a - b);
console.log(ordered.join(", "));
console.log(durations.join(", "));5, 30, 100
30, 5, 100Try duplicate values and an empty array. Then adapt the comparator to records with a minutes property.
Recall
Which operation returns an object, which returns a boolean, and which reorders a list? What must you check after find?
Completion is saved in this browser only. It does not measure mastery.