Fix an off-by-one loop
This function throws when the array contains no string longer than minLength. Find the boundary error.
function firstLongerThan(items, minLength) {
for (let i = 0; i <= items.length; i++) {
if (items[i].length > minLength) return items[i];
}
return null;
}
The final valid index is one less than the array’s length. At i === items.length, items[i] is undefined.
Which change prevents the error when no item is long enough?
Why it matters: Boundary mistakes are a common source of runtime errors when iterating over indexed data.