Chapter 3 · Data

Callbacks: functions on arrays

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 22 · Concept

Give a function, let another call it

By the end of this lesson you hand functions to arrays to make new arrays, choose, total and sort.

const scores = [72, 45, 90];
scores.forEach((s) => console.log(s));

A callback is a function you give to another function to call. Here forEach calls the arrow function once per item, in order.

const scores = [72, 45, 90];
scores.forEach((s) => console.log(s));
for (const s of scores) {
  console.log(s);
}
const prices = [2, 3.5];
const doubled = prices.map((p) => p * 2);
console.log(doubled.join(" "));
console.log(prices.join(" "));
const scores = [72, 45, 90];
const limit = 50;
const passed = scores.filter((s) => s >= limit);
console.log(passed.join(", "));
console.log(scores.find((s) => s < 50));
console.log(scores.find((s) => s > 100));
console.log(scores.some((s) => s > 80));
console.log(scores.every((s) => s > 80));
const students = [
  { name: "Ada", score: 72 },
  { name: "Bob", score: 45 },
  { name: "Cy", score: 90 },
];
// 1. filter: the students with 50 or more
// 2. map: each of those to its name
// 3. log Passed: and the names, joined
const scores = [72, 45, 90, 38];
const report = scores
  .filter((s) => s >= 40)
  .map((s) => (s >= 50 ? `${s} pass` : `${s} fail`));
console.log(report.join(", "));
const total = scores.reduce((sum, s) => sum + s, 0);
console.log("Total:", total);
const cart = [
  { name: "tea", price: 2.5 },
  { name: "cake", price: 3.1 },
  { name: "jam", price: 1.75 },
];
// 1. add up the prices with reduce
// 2. log the total with two decimals
// 3. log yes or no for free delivery
const xs = [10, 9, 1];
xs.sort();
console.log(xs.join(" "));
const xs = [10, 9, 1, 25];
console.log(xs.toSorted((a, b) => a - b).join(" "));
console.log(xs.join(" "));
xs.sort((a, b) => b - a);
console.log(xs.join(" "));
console.log("Max:", Math.max(...xs));
const people = [
  { name: "Cy", score: 90 },
  { name: "Ada", score: 70 },
  { name: "Bob", score: 80 },
];
people.sort((a, b) => b.score - a.score);
console.log(people.map((p) => p.name).join(" "));
people.sort((a, b) => a.name.localeCompare(b.name));
console.log(people.map((p) => p.name).join(" "));
const bad = [3, 1, 2, 5, 4];
bad.sort((a, b) => a > b);
console.log(bad.join(" "));
const players = [
  { name: "Bob", score: 85 },
  { name: "Cy", score: 90 },
  { name: "Di", score: 70 },
  { name: "Ada", score: 90 },
];
// 1. sort the players, highest score first
// 2. log each player's name and score
const students = [
  { name: "Di", score: 95 },
  { name: "Bob", score: 45 },
  { name: "Ada", score: 80 },
  { name: "Cy", score: 95 },
];
// 1. the average score
// 2. the names with 50 or more, by name
// 3. the best score
const players = [
  { name: "Bob", score: 85 },
  { name: "Cy", score: 90 },
  { name: "Di", score: 70 },
  { name: "Ada", score: 90 },
];
players.sort((a, b) => b.score - a.score);
players.forEach((p) => console.log(`${p.name} ${p.score}`));
Terminal
Step 1 of 11forEach and for...of

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)