The patterns you will use forever
Most array work reduces to a handful of loop shapes you already met in Python. Here they are in JavaScript, using the for...of loop from lesson 3-3.
Sum (Python's sum(xs) has no built-in here):
let total = 0; for (const s of scores) { total = total + s; }
Biggest so far:
let best = scores[0]; for (const s of scores) { if (s > best) best = s; }
Count matches: start a counter at 0 and add 1 inside an if.
When the body is a single statement, the braces may be dropped, as in if (s > best) best = s;. In unit 7 you will replace several of these hand-written loops with one-liners like reduce, but the loop versions must feel easy first.
Code exercise · javascript
Run the sum and max patterns over the scores array. The average divides the total by the length.
Code exercise · javascript
Your turn. Count how many scores are passing (70 or higher) and print the count. Use the count-matches pattern: a counter starting at 0, plus 1 inside an if.
Code exercise · javascript
Second practice. Find the LOWEST temperature: it is the biggest-so-far pattern flipped around. Start with the first item, replace it whenever you meet something smaller, then print the result.
Problem
Trace by hand, no running: const a = [1, 2, 3]; a.push(4); a.pop(); a.pop(); What is a.length now?