A for loop repeatedly runs a block while advancing through an array or range. Its logic can often be expressed more clearly with functional tools. section 13.6 section 19.9
Refactor an existing loop by following its data flow:
- Read the loop and restate its behavior as a series of steps. section 13.6
- Use clues: one output per input suggests map(), while combining values suggests reduce(). section 13.6
- Make a subarray, operate on the whole array, and take small steps; reuse average() where appropriate. section 13.8
A nested-loop listing calculates moving averages, exposing the indexing, accumulation, and output steps that can be refactored. section 13.6


























