ProgramGuru

Array Programs

Array Programs — purpose and prerequisites

Array programs apply typed collections, map, filter, reduce, and non-mutating operations. A function accepting readonly number[] communicates that it should not modify the caller's collection. Always decide how an empty array should behave.

What you’ll learn

  • Explain the purpose of array programs.
  • Identify the typed contract, runtime responsibilities and common pitfalls.
  • Run the example and extend its behavior with a practical exercise.

How the idea works

The reduction adds every item and filter selects positive values. Neither method mutates the original array.

Array.sort() mutates its input and compares strings by default unless a numeric comparator is supplied.

Visualize the flow

Array Programs — learning flowArray items → Aggregate + filter → Summary objectArray Programs1Array items2Aggregate + filter3Summary objectRun the example and test a boundary case.
A three-step view of the responsibilities and the observable result.

Run the checked TypeScript example

This source was compiled using TypeScript 5.8.3 with strict checking, then executed with Node.js 22.16.0. Its stdout below is the actual captured output. The core logic is runnable without a framework or a remote API; integration-specific examples appear in a separate lab when needed.

TypeScript source

function summarize(values: readonly number[]): { total: number; positives: number } {
  return {
    total: values.reduce((sum, value) => sum + value, 0),
    positives: values.filter(value => value > 0).length,
  };
}
console.log(summarize([4, -2, 7, 0]));

Verified output

{ total: 9, positives: 2 }

Compile: npx tsc --strict --target es2022 --module commonjs array-programs.ts; run: node array-programs.js. For examples importing node: modules, install @types/node as a development dependency; for browser or React projects use the separate lab setup.

Trace the execution

The reduction adds every item and filter selects positive values. Neither method mutates the original array.

Before editing the code, predict what the output will be for one changed input. Run it and compare your prediction with the actual result.

Common mistake and correction

Array.sort() mutates its input and compares strings by default unless a numeric comparator is supplied.

Determine whether the issue belongs to the type system, a trust boundary, or actual runtime behavior before choosing a correction.

Your turn: extend the example

Return the median of an array using a copied sorted array and handle empty input explicitly.

Record expected success and failure outcomes, run the code, and compare the observed behavior. Keep the exercise compatible with strict type checking.

Check your understanding

Which statement correctly describes array programs in the example?
Choose one answer. You can try again.

Answer and explanation

The reduction adds every item and filter selects positive values.

The reduction adds every item and filter selects positive values. Neither method mutates the original array.

Key idea to remember

The reduction adds every item and filter selects positive values. Neither method mutates the original array.

Apply the same reasoning to the practice task, then integrate it into a real app only when the runtime and dependency setup are ready.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Tuple Programs →