Calling Functions
The full text of this chapter, to read or revisit. To practise, do the exercises.
βΆ Do the exercisesExercise 1 β Call It as Often as You Like
Once declared, a function can be called any number of times. Each call runs the whole body again, from the top:
function cheer() {
console.log("Go Aquachu, go!");
}
cheer();
cheer();
cheer();And since a call is just a line of code, it can live inside a loop:
function cheer() {
console.log("Go Aquachu, go!");
}
for (let i = 0; i < 5; i++) {
cheer();
}Exercise 2 β Calling Before Declaring
Surprising but true: a function declared with `function` can be called before its declaration. JavaScript reads all declarations first, then runs the code.
startBattle();
function startBattle() {
console.log("A wild Rockachu appears!");
}Most people still declare functions at the top and call them below: it's easier to read. But it's good to know the order isn't an error.
Exercise 3 β Quiz: How Many Times?
How many lines does this code print?
function train() {
console.log("Training...");
}
for (let day = 1; day <= 3; day++) {
train();
}
train();4315Hint : Count the calls inside the loop, then don't forget what comes after it.
Exercise 4 β Three Sessions
Make the training run exactly three times.
Hint : The loop decides how many times, and the call needs its usual ending.
Exercise 5 β Functions Calling Functions
A function body can contain calls to other functions. This lets you build big actions out of small ones:
function showIntro() {
console.log("A wild Florachu appears!");
}
function chooseCodemon() {
console.log("Go, Pyrochu!");
}
function startBattle() {
showIntro();
chooseCodemon();
console.log("Battle start!");
}
startBattle();Calling `startBattle` runs `showIntro`, then `chooseCodemon`, then the last line. Each call finishes completely before the next line runs.
Exercise 6 β Quiz: In What Order?
What does this code print?
function second() {
console.log("B");
}
function first() {
console.log("A");
second();
console.log("C");
}
first();A, B, CA, C, BB, A, CA onlyHint : Follow the calls line by line, entering each function when it's called.
Exercise 7 β The Forgotten Parentheses
Writing the name without parentheses doesn't call the function. JavaScript just looks at it and moves on, without any error:
function attack() {
console.log("Zap uses Spark!");
}
attack; // nothing happens
attack(); // this runs itOnly one message, from the second line. If a function "doesn't work", check the parentheses first.
Exercise 8 β Battle Rounds
Run round one, then round one again, then round two.
Hint : Follow the requested order exactly, one call per line.
Exercise 9 β Call or Not?
Match each line with what it does.
train();train;train(); inside a loop of 5 turnsExercise 10 β Battle Script
Write the script of a battle out of small functions. 1. Declare `intro` that prints `"A wild Metachu appears!"` 2. Declare `attack` that prints `"Pyrochu uses Flame!"` 3. Declare `outro` that prints `"Metachu is KO!"` 4. Declare `battle` that runs the intro, then three attacks, then the outro 5. Call `battle` once
// Write your code here