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?
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!");
}Roar!undefinedHint : 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();4231Hint : 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();Hello! onceHello! twiceHint : Only one of the two lines is an actual call.
Exercise 5 β Quiz: Parameter or Argument
In `function attack(power)`, what is `power`?
Hint : This word appears in the declaration.
Exercise 6 β Quiz: Pass the Name
Complete the function so it greets whoever is passed to it.
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");B / AA / Bfirst / secondundefined / undefinedHint : 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();[undefined][][name]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);797 8 9Hint : 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);12undefinedn * 34Hint : 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.
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);Go! then undefinedGo! twiceGo! onlyundefined onlyHint : 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));KOOKKO then OKundefinedHint : 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());1after then 1afterHint : 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.
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)));12693Hint : 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);PyrochuundefinedHint : 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();ZapundefinedheroHint : 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);1100undefinedHint : 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);1001undefinedHint : 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.
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);10 then an error10 twice10 then undefinedHint : 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();outer then innerinner then outerouter onlyinner onlyHint : 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!");
}Ready!undefinedHint : 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));5052030undefinedHint : Each call becomes a number before the addition.