Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
βΆ Do the exercisesExercise 1 β Quiz: Which Is an Expression?
Which line is a function expression?
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");
};RunningundefinedHint : 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");
}RunningundefinedHint : 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));9undefineda + b45Hint : 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));undefined9a + bHint : 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?
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();undefined40zapHint : 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());10undefined0Hint : 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));251035undefinedHint : 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);0 then 77 twice0 twice7 then 0Hint : 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);"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());A!undefinedbHint : 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?
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"));4105Hint : 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 + "!"));a! then b!x! twicea!b!undefinedHint : 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));93n * nundefinedHint : The result of the callback is returned by apply.
Exercise 20 β Quiz: Anonymous
Which call uses an anonymous function?
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"));[, 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]());ThunderSparkundefinedHint : 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));869undefinedHint : 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();40undefinedzapHint : The method shorthand keeps this bound to the object.