Testing a function with console.assert

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Testing a function with console.assert

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does console.assert(add(2, 3) === 5) print when add(2, 3) returns 5?
A Nothing, because the condition is true
B It prints true
2. When does console.assert(condition, message) print the message?
A Only when the condition is true
B Only when the condition is false
3. Which line is a test that checks add(2, 3) gives the expected result of 5?
A console.assert(add(2, 3) = 5)
B console.assert(add(2, 3) === 5)
4. In console.assert(double(4) === 8, "double failed"), what is the expected result?
A 8
B double(4)
5. Why might a programmer write several console.assert lines in a row?
A To check the function with several different test cases
B To make the function run faster

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1console.assert(add(2, 3) === 5) AThe arguments are in the wrong order. The condition must come first and the message second.
2console.assert(isEven(4) === false, "isEven(4) failed") BA single = assigns a value instead of comparing. The test should use === to compare the actual result with the expected result.
3Several test cases in a row check one function with different inputs, and each failing test prints its own message Cconsole.assert(square(3) === 9);
4Expected versus actual: the function call is the actual result and it is compared with the expected value using === Dconsole.assert(double(2) === 4, "double(2)"); console.assert(double(0) === 0, "double(0)");
5console.assert(add(2, 3) = 5) ETests that add(2, 3) gives the expected result 5; it stays silent if the test passes and prints an error if it fails
6console.assert("add(2, 3) failed", add(2, 3) === 5) FPrints the message "isEven(4) failed" only if isEven(4) does not return false

Definitions

console.assert(condition, message)

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
______________(add(2, 3) === 5);
2.
console.assert(add(2, 3) ___ 5, "2+3");
3.
console.assert(add(2, 3) === 5); console.assert(___(0, 0) === 0);
4.
console.assert(double(4) === _, "double");
5.
const actual = add(4, 6); const expected = 10; console.assert(actual === ________, "add");

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A const sq = n => n * n;
B console.assert(sq(3) = 9);
C console.assert(sq(4) === 16);
Question 2
A console.asert(sq(5) === 25);
B console.assert(sq(2) === 4);
C function sq(n) {
D return n * n;
E }
Question 3
A const got = double[4];
B const want = 8;
C console.assert(got === want);
D function double(n) {
E return n * 2;
F }
Question 4
A function isEven(n) {
B return n % 2 === 0;
C }
D console.assert(isEven(4);
E console.assert(!isEven(7));
Question 5
A const add = (a, b) => a + b;
B const s = add(2, 3);
C console.assert('add', s===5);

Answer Key

Testing a function with console.assert
Section A: Quick Questions
Q1
A — Nothing, because the condition is true
Q2
B — Only when the condition is false
Q3
B — console.assert(add(2, 3) === 5)
Q4
A — 8
Q5
A — To check the function with several different test cases
Section B: Match the Pairs
Q1
console.assert(add(2, 3) === 5) → Tests that add(2, 3) gives the expected result 5; it stays silent if the test passes and prints an error if it fails
Q2
console.assert(isEven(4) === false, "isEven(4) failed") → Prints the message "isEven(4) failed" only if isEven(4) does not return false
Q3
Several test cases in a row check one function with different inputs, and each failing test prints its own message → console.assert(double(2) === 4, "double(2)"); console.assert(double(0) === 0, "double(0)");
Q4
Expected versus actual: the function call is the actual result and it is compared with the expected value using === → console.assert(square(3) === 9);
Q5
console.assert(add(2, 3) = 5) → A single = assigns a value instead of comparing. The test should use === to compare the actual result with the expected result.
Q6
console.assert("add(2, 3) failed", add(2, 3) === 5) → The arguments are in the wrong order. The condition must come first and the message second.
Section C: Definitions
Q1
console.assert(condition, message) — Prints the message only when the condition is false, so a test like console.assert(add(2, 3) === 5) stays silent when the function gives the expected result.
Section D: Fill in the Blanks
Q1
console.assert(add(2, 3) === 5);
Q2
console.assert(add(2, 3) === 5, "2+3");
Q3
console.assert(add(2, 3) === 5); console.assert(add(0, 0) === 0);
Q4
console.assert(double(4) === 8, "double");
Q5
const actual = add(4, 6); const expected = 10; console.assert(actual === expected, "add");
Section E: Spot the Error
Q1
B — A single = assigns a value instead of comparing, so this is not a real test (and 9 can't be assigned to a function call). The condition must use === like this: console.assert(sq(3) === 9);
Q2
A — The method name is misspelled. It must be console.assert with two s letters, otherwise JavaScript throws an error and the test never runs.
Q3
A — Square brackets are used for array indexes, not for calling a function. To get the actual result you must call the function with round brackets: double(4).
Q4
D — The closing bracket of console.assert( is missing. The line should end with two closing brackets: console.assert(isEven(4));
Q5
C — The arguments are in the wrong order. The condition must come first and the message second. Here the text 'add' is the condition, so it is always truthy and the test can never fail.