Skip to content
Exit presentation

Data Handling I: Organizing & Visualizing Data

A list the program builds itself

The kitchen register lists the plates each class needs. It has six rows, one per class. The program reads it row by row and builds a list.

Loading lab…

let meals = []; — the list starts empty.
Step 1 of 7
  1. let meals = []; — the list starts empty.
  2. Push 6A's count. One entry.
  3. Push 6B's count. Two entries.
  4. Push 7A's count. Three entries.
  5. Push 7B's count. Four entries.
  6. Push 8A's count. Five entries.
  7. Push 8B's count. Six entries.

The rule

.push() adds one value to the end of a list. So a program can build a list while it runs. Where the empty list is made decides what ends up in it.

`` let meals = []; for (const name of register) { const count = platesFor(name); meals.push(count); } ``

register holds the six class names. platesFor(name) looks up one class's plate count. The for loop repeats its lines once for each name.

Run it and meals holds six counts, in register order.

The line let meals = []; must run once, before the loop. If it sits inside the loop, it runs once per class. Every push then goes into a brand-new list. So the list ends with just one count.

Jump to lesson

This track

  1. 0 Start here
  2. 1 One list holds the whole week
  3. 2 Every item has a position
  4. 3 A list the program builds itself
  5. 4 One box outside the loop
  6. 5 Total divided by count
  7. 6 The biggest, without sorting
  8. 7 One count per group
  9. 8 The chart the program drew
  10. 9 The big picture
  11. 10 Track quiz

Other tracks