Javascript · Functions (Advanced)
Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — Quiz: Which Is an Expression?
Which line is a function expression?
Options
• ```const go = function () { };```
• ```function go() { }```
• ```go();```
• ```const go = go();```
Hint : A nameless function assigned to a variable.
Exercise 2 — Quiz: Expression Called Early
What happens when this code runs?
run(); const run = function () { console.log("Running"); };
Options
• An error
• ```Running```
• Nothing
• ```undefined```
Hint : A const doesn't exist before its line.
Exercise 3 — Quiz: Declaration Called Early
What does this code print?
run(); function run() { console.log("Running"); }
Options
• ```Running```
• An error
• Nothing
• ```undefined```
Hint : Declarations are read before the code runs.
Exercise 4 — Quiz: Arrow Result
What does this code print?
const add = (a, b) => a + b; console.log(add(4, 5));
Options
• ```9```
• ```undefined```
• ```a + b```
• ```45```
Hint : No braces: the expression is returned.
Exercise 5 — Quiz: Braces Without return
What does this code print?
const add = (a, b) => { a + b; }; console.log(add(4, 5));
Options
• ```undefined```
• ```9```
• ```a + b```
• An error
Hint : With braces, nothing comes back unless you ask for it.
Exercise 6 — Quiz: Shortest Arrow
Write `half` in the shortest arrow form.
const half = (n) ____ n ____ 2; console.log(half(10));
Options: => / return * ->
Hint : Arrow, then the single expression that computes a half.
Exercise 7 — Quiz: One Parameter
Which arrow function is written correctly?
Options
• ```const f = n => n + 1;```
• ```const f = n + 1 => n;```
• ```const f = => n + 1;```
• ```const f = (n) n + 1;```
Hint : Parameter, arrow, expression: in that order.
Exercise 8 — Quiz: Arrow as Method
Inside `show`, what is `this.hp`?
const zap = { hp: 40, show: () => { console.log(this.hp); } }; zap.show();
Options
• ```undefined```
• ```40```
• ```zap```
• An error
Hint : Arrow functions don't get their own this.
Exercise 9 — Quiz: Default Applied
What does this code print?
function hit(damage = 10) { return damage; } console.log(hit());
Options
• ```10```
• ```undefined```
• ```0```
• An error
Hint : Nothing was passed, so the default steps in.
Exercise 10 — Quiz: Default Ignored
What does this code print?
function hit(damage = 10) { return damage; } console.log(hit(25));
Options
• ```25```
• ```10```
• ```35```
• ```undefined```
Hint : A passed value always wins over the default.
Exercise 11 — Quiz: Add a Default
Make `greet()` print `Hello trainer` when no name is passed.
function greet(name ____ ____) { console.log("Hello " + name); } greet(); greet("Sam");
Options: = "trainer" == trainer :
Hint : A default is an assignment in the parameter list, and text needs quotes.
Exercise 12 — Quiz: Zero and Defaults
What does this code print?
function show(n = 7) { console.log(n); } show(0); show(undefined);
Options
• ```0``` then ```7```
• ```7``` twice
• ```0``` twice
• ```7``` then ```0```
Hint : Only one of the two arguments counts as "nothing".
Exercise 13 — Quiz: typeof
What does this code print?
const f = () => 1; console.log(typeof f);
Options
• ```"function"```
• ```"object"```
• ```"number"```
• ```"arrow"```
Hint : Functions have a type of their own.
Exercise 14 — Quiz: Copying a Function
What does this code print?
const a = () => "A!"; const b = a; console.log(b());
Options
• ```A!```
• ```undefined```
• ```b```
• An error
Hint : Both names point to the same function.
Exercise 15 — Quiz: Pass, Don't Call
`run` expects a function and calls it. Which call is correct?
Options
• ```run(cheer);```
• ```run(cheer());```
• ```run("cheer");```
• ```run(cheer)();```
Hint : Parentheses would run it too early.
Exercise 16 — Quiz: Give the Function
Complete the code so `twice` runs `hello` two times.
function twice(action) { ____(); action(); } const hello = () => console.log("Hello"); twice(____);
Options: action hello hello() twice "hello"
Hint : Inside, the parameter name; outside, the function itself.
Exercise 17 — Quiz: Callback Count
How many times is `Hit` printed?
function repeat(times, action) { for (let i = 0; i < times; i++) { action(); } } repeat(4, () => console.log("Hit"));
Options
• ```4```
• ```1```
• ```0```
• ```5```
Hint : The loop decides how many times the callback runs.
Exercise 18 — Quiz: Callback Argument
What does this code print?
function each(list, action) { for (let item of list) { action(item); } } each(["a", "b"], (x) => console.log(x + "!"));
Options
• ```a!``` then ```b!```
• ```x!``` twice
• ```a!b!```
• ```undefined```
Hint : Each item is handed to the callback in turn.
Exercise 19 — Quiz: Callback Return
What does this code print?
function apply(v, f) { return f(v); } console.log(apply(3, (n) => n * n));
Options
• ```9```
• ```3```
• ```n * n```
• ```undefined```
Hint : The result of the callback is returned by apply.
Exercise 20 — Quiz: Anonymous
Which call uses an anonymous function?
Options
• ```run(() => console.log(1));```
• ```run(show);```
• ```run(show());```
• ```run();```
Hint : Look for a function written directly in the call.
Exercise 21 — Quiz: Inline It
Pass an anonymous arrow function that prints `Tick` directly to `repeat`.
function repeat(times, action) { for (let i = 0; i < times; i++) { action(); } } repeat(2, ____ ____ console.log("Tick"));
Options: () => function tick {}
Hint : Empty parentheses, then the arrow.
Exercise 22 — Quiz: Order With Callbacks
What does this code print?
function wrap(action) { console.log("["); action(); console.log("]"); } wrap(() => console.log("x"));
Options
• ```[```, ```x```, ```]```
• ```x```, ```[```, ```]```
• ```[```, ```]```, ```x```
• ```[```, ```]```
Hint : Follow the body of wrap from top to bottom.
Exercise 23 — Quiz: Function in an Array
What does this code print?
const moves = [() => "Spark", () => "Thunder"]; console.log(moves[1]());
Options
• ```Thunder```
• ```Spark```
• The function itself
• ```undefined```
Hint : Index first, then call.
Exercise 24 — Quiz: Expression With Parameters
What does this code print?
const power = function (base, exp) { return base ** exp; }; console.log(power(2, 3));
Options
• ```8```
• ```6```
• ```9```
• ```undefined```
Hint : It's a normal function stored in a variable.
Exercise 25 — Quiz: Method With this
What does this code print?
const zap = { hp: 40, show() { console.log(this.hp); } }; zap.show();
Options
• ```40```
• ```undefined```
• ```zap```
• An error
Hint : The method shorthand keeps this bound to the object.
Back to the course