Javascript · Functions (Basics)
Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — Quiz: Valid Declaration
Which line correctly declares a function?
Options
• ```function heal() {```
• ```function heal {```
• ```heal() function {```
• ```def heal() {```
Hint : Keyword, name, parentheses, block: in that order.
Exercise 2 — Quiz: Never Called
What does this code print?
function roar() {
console.log("Roar!");
}
Options
• Nothing
• ```Roar!```
• ```undefined```
• An error
Hint : Declaring and running are two different steps.
Exercise 3 — Quiz: Count the Lines
How many lines are printed?
function step() {
console.log("Step");
}
for (let i = 0; i < 2; i++) {
step();
}
step();
step();
Options
• ```4```
• ```2```
• ```3```
• ```1```
Hint : Add the calls inside the loop to the ones outside.
Exercise 4 — Quiz: Missing Parentheses
What does this code print?
function hello() {
console.log("Hello!");
}
hello;
hello();
Options
• ```Hello!``` once
• ```Hello!``` twice
• Nothing
• An error
Hint : Only one of the two lines is an actual call.
Exercise 5 — Quiz: Parameter or Argument
In `function attack(power)`, what is `power`?
Options
• A parameter
• An argument
• A return value
• A global variable
Hint : This word appears in the declaration.
Exercise 6 — Quiz: Pass the Name
Complete the function so it greets whoever is passed to it.
function greet(____) {
console.log("Hi " + ____);
}
greet("Florachu");
Options: who who "who" greet
Hint : The same name is declared in the parentheses and used in the body.
Exercise 7 — Quiz: Argument Order
What does this code print?
function show(first, second) {
console.log(second + " / " + first);
}
show("A", "B");
Options
• ```B / A```
• ```A / B```
• ```first / second```
• ```undefined / undefined```
Hint : Map each argument to its position, then read the body carefully.
Exercise 8 — Quiz: Nothing Passed
What does this code print?
function tag(name) {
console.log("[" + name + "]");
}
tag();
Options
• ```[undefined]```
• ```[]```
• ```[name]```
• An error
Hint : A parameter without an argument still exists, with a default value.
Exercise 9 — Quiz: Too Many
What does this code print?
function one(x) {
console.log(x);
}
one(7, 8, 9);
Options
• ```7```
• ```9```
• ```7 8 9```
• An error
Hint : Extra arguments have nowhere to go.
Exercise 10 — Quiz: Stored Result
What does this code print?
function triple(n) {
return n * 3;
}
let r = triple(4);
console.log(r);
Options
• ```12```
• ```undefined```
• ```n * 3```
• ```4```
Hint : The call is replaced by what the function returns.
Exercise 11 — Quiz: Give It Back
Complete the function so that the call returns the sum.
function add(a, b) {
____ a + b;
}
let total = add(10, 5);
console.log(total);
Options: return console.log let print
Hint : Printing and returning are two different things.
Exercise 12 — Quiz: No return
What does this code print?
function shout(word) {
console.log(word + "!");
}
let result = shout("Go");
console.log(result);
Options
• ```Go!``` then ```undefined```
• ```Go!``` twice
• ```Go!``` only
• ```undefined``` only
Hint : The function prints, but what does it return?
Exercise 13 — Quiz: Early Exit
What does this code print?
function check(hp) {
if (hp <= 0) {
return "KO";
}
return "OK";
}
console.log(check(0));
Options
• ```KO```
• ```OK```
• ```KO``` then ```OK```
• ```undefined```
Hint : The first return that runs ends everything.
Exercise 14 — Quiz: Unreachable
What does this code print?
function value() {
return 1;
console.log("after");
}
console.log(value());
Options
• ```1```
• ```after``` then ```1```
• ```after```
• An error
Hint : What happens to lines placed after a return?
Exercise 15 — Quiz: Is It Ready?
Complete the function so it answers whether the level is at least 5, then use it.
function isReady(level) {
____ level ____ 5;
}
if (isReady(7)) {
console.log("Ready!");
}
Options: return >= console.log ==
Hint : "At least 5" includes 5, and the answer must come back to the condition.
Exercise 16 — Quiz: Inside Out
What does this code print?
function twice(n) {
return n * 2;
}
console.log(twice(twice(3)));
Options
• ```12```
• ```6```
• ```9```
• ```3```
Hint : Solve the inner call first.
Exercise 17 — Quiz: Local Variable
What happens on the last line?
function pick() {
let choice = "Pyrochu";
}
pick();
console.log(choice);
Options
• An error
• ```Pyrochu```
• ```undefined```
• Nothing
Hint : Where does a variable declared in a function live?
Exercise 18 — Quiz: Reading a Global
What does this code print?
let hero = "Zap";
function show() {
console.log(hero);
}
show();
Options
• ```Zap```
• ```undefined```
• An error
• ```hero```
Hint : Functions can look outward.
Exercise 19 — Quiz: Shadowed
What does the last line print?
let count = 1;
function bump() {
let count = 100;
}
bump();
console.log(count);
Options
• ```1```
• ```100```
• ```undefined```
• An error
Hint : A let inside the function creates its own variable.
Exercise 20 — Quiz: Really Modified
What does the last line print?
let count = 1;
function bump() {
count = 100;
}
bump();
console.log(count);
Options
• ```100```
• ```1```
• ```undefined```
• An error
Hint : No let this time: the assignment targets an existing variable.
Exercise 21 — Quiz: Call It Three Times
Make the function run once per turn of the loop.
function tick() {
console.log("Tick");
}
for (let i = 0; i < 3; i++) {
________;
}
Options: tick () [] "tick" ;
Hint : Name, then the sign that triggers the call.
Exercise 22 — Quiz: Parameter Scope
What happens with this code?
function heal(amount) {
console.log(amount);
}
heal(10);
console.log(amount);
Options
• ```10``` then an error
• ```10``` twice
• ```10``` then ```undefined```
• Nothing
Hint : A parameter only exists while its function runs.
Exercise 23 — Quiz: Which Runs First?
What does this code print?
function inner() {
console.log("inner");
}
function outer() {
console.log("outer");
inner();
}
outer();
Options
• ```outer``` then ```inner```
• ```inner``` then ```outer```
• ```outer``` only
• ```inner``` only
Hint : Follow the body of the called function line by line.
Exercise 24 — Quiz: Called Early
What does this code print?
ready();
function ready() {
console.log("Ready!");
}
Options
• ```Ready!```
• An error
• Nothing
• ```undefined```
Hint : JavaScript reads declarations before running anything.
Exercise 25 — Quiz: Using the Result
What does this code print?
function bonus(level) {
return level * 10;
}
console.log(bonus(2) + bonus(3));
Options
• ```50```
• ```5```
• ```2030```
• ```undefined```
Hint : Each call becomes a number before the addition.
Back to the course