So far, you’ve built four loop patterns. A running total added up song lengths, a counter tracked how many items passed a test, and a new list kept those items. In the video, firstStartingWith(songs, letter) stopped at the first match and returned undefined when nothing matched.
Each loop answers a different question. This lesson adds three more: which item is the largest or smallest, whether any item passes a test, and whether all items do. For each, you’ll decide what the function returns for an empty list, and at the end you’ll choose a pattern for a fresh task before writing it.
Add Up a Week of Walks
Say you’ve decided to walk more, and for one week you jot down how many minutes you walked each day. Monday was 25 minutes, Tuesday 40, and Wednesday it rained, so that day gets a 0. You can answer the first question, how long you walked in total, with the running total pattern you met with the playlist. Create walks.js in your course folder.
walks.js
function totalWalked(minutes) {
let total = 0;
for (const walk of minutes) {
total = total + walk;
}
return total;
}
const week = [25, 40, 0, 35, 60, 15, 30];
console.log(totalWalked(week));
console.log(totalWalked([]));
Predict both numbers, then run the script.
$ node walks.js
205
0
A variable like total is an accumulator: it starts with a value before the loop and collects something from every item. The counter from Count and Select Matching Items is one too. Only the update differs. A counter adds 1 on a matching pass, and a total adds the item itself on every pass.
The rainy Wednesday is still an item, and the loop visits it like any other day. For an empty week, the function returns 0, the starting value. With no walks, you walked zero minutes.
Average the Walks That Count
An average is a total divided by how many items went into it. Say only walks of at least 30 minutes count as workouts, and you want their average. That takes two accumulators, a total and a count. Create average.js in your course folder.
average.js
function averageAtLeast(minutes, goal) {
let total = 0;
let count = 0;
for (const walk of minutes) {
if (walk >= goal) {
total = total + walk;
count = count + 1;
}
}
if (count === 0) {
return undefined;
}
return total / count;
}
const week = [25, 40, 0, 35, 60, 15, 30];
console.log(averageAtLeast(week, 30));
console.log(averageAtLeast([], 30));
$ node average.js
41.25
undefined
Four walks reach 30 minutes and add up to 165, so the average is 41.25. Both updates sit inside the same if, so the count always matches the walks in the total.
The check after the loop matters when no walk matches: total / count would then be 0 / 0, which gives NaN. An average needs at least one item, so the function returns undefined instead.
Find the Longest Walk
Sometimes you want one standout day, such as your longest walk. Finding the largest or smallest item in a list is the extreme pattern.
The plan is to keep a “longest so far” variable and compare each walk with it. A longer walk replaces it, so after the loop, it holds the longest walk of all. Before the loop, you haven’t compared any walks, but the first walk is certainly a candidate, so the pattern starts there. Create longest.js in your course folder.