Callbacks (Intro)
The full text of this chapter, to read or revisit. To practise, do the exercises.
βΆ Do the exercisesExercise 1 β What Is a Callback?
In the previous chapter, you passed a function to another function. This has a name: the function you pass is a callback. Here's a classic example: `repeat` takes a number and an action, and runs that action as many times as asked.
function repeat(times, action) {
for (let i = 0; i < times; i++) {
action();
}
}
const cheer = () => console.log("Go Pyrochu!");
repeat(3, cheer);`repeat` doesn't know what `action` does. It only knows when to call it. That separation is the whole point: the same `repeat` works with any action.
Exercise 2 β Callbacks With Parameters
The receiving function can pass arguments to the callback when it calls it. Here each member of the team is handed to the callback one by one:
function forEachMember(team, action) {
for (let cdm of team) {
action(cdm);
}
}
const greet = (name) => console.log("Hello " + name + "!");
forEachMember(["Aquachu", "Zap"], greet);Exercise 3 β Quiz: Who Calls Whom?
In `repeat(3, cheer)`, which statement is true?
Hint : The callback is handed over, then called from inside the other function.
Exercise 4 β Repeat With a Counter
Complete `countdown` so it passes the current number to the callback, then use `show` as the callback.
Hint : Pass the loop variable to the callback, and pass the function without calling it.
Exercise 5 β Using the Callback's Result
A callback can return a value, and the receiving function can use it. Here the callback decides which members count:
function countIf(values, test) {
let count = 0;
for (let v of values) {
if (test(v)) {
count++;
}
}
return count;
}
const levels = [3, 12, 7, 15];
const isHigh = (level) => level >= 10;
console.log(countIf(levels, isHigh));`countIf` doesn't know what "high" means. The callback does. Swap it for another test and the same function counts something else:
Exercise 6 β Quiz: Counting With a Callback
What does this code print?
function countIf(values, test) {
let count = 0;
for (let v of values) {
if (test(v)) {
count++;
}
}
return count;
}
const isSmall = (n) => n < 5;
console.log(countIf([1, 9, 4, 6, 2], isSmall));325trueHint : Apply the test to each value and count the true answers.
Exercise 7 β Why Callbacks Matter
Callbacks let you write a function once and customise its behaviour at each call. Compare: without a callback, you'd need one function per case.
function transformAll(values, transform) {
const result = [];
for (let v of values) {
result.push(transform(v));
}
return result;
}
const double = (n) => n * 2;
const addTen = (n) => n + 10;
console.log(transformAll([1, 2, 3], double));
console.log(transformAll([1, 2, 3], addTen));Coming next
JavaScript arrays have built-in functions that work exactly like `forEachMember`, `countIf` and `transformAll`. They're called `forEach`, `filter` and `map`, and they're the subject of the next class.
Exercise 8 β Custom Test
Complete `keepIf` so it adds a value to the result only when the callback says so, then count the electric moves.
Hint : Inside, the callback goes by its parameter name. Outside, pass the function itself.
Exercise 9 β Callback Vocabulary
Match each term with its description.
repeat(3, cheer)action(cdm)Exercise 10 β Quiz: Order of Output
What does this code print?
function twice(action) {
console.log("start");
action();
action();
console.log("end");
}
twice(() => console.log("hit"));start, hit, hit, endhit, hit, start, endstart, end, hit, hitstart, hit, endHint : Follow the body of twice line by line.
Exercise 11 β Team Processor
Write a generic function that runs a callback on every member of a team, then use it with two different callbacks.
Step 1: `processTeam(team, action)` calls `action` once per member, passing the member's name and its position counted from 1. Step 2: write a callback `announce(name, position)` that prints `"position. name"`, for example `"1. Aquachu"`. Step 3: write a callback `cheer(name, position)` that prints `"Go name!"`. Step 4: run `processTeam` with `announce`, then with `cheer`.
Expected output:
1. Aquachu
2. Pyrochu
3. Zap
Go Aquachu!
Go Pyrochu!
Go Zap!const team = ["Aquachu", "Pyrochu", "Zap"];
// Write your functions here
// Use them here