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.

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.

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.

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`.

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