← ACARA Digital Technologies Years 7–8

Testing a function with expected and actual output

Test a function by calling it with chosen inputs, logging the actual result next to the expected value, then recording pass or fail. Retest after any fix.

Understanding Testing a function with expected and actual output in ACARA Digital Technologies Years 7–8

What it does: 6 is a typical input, and the log shows the expected value 36 next to what square(6) really returns. Retesting means calling the function with the same input after the fix and checking that actual now matches expected. If you copy the output as the expected value, the test always passes, even when the function is wrong. Boundary inputs sit right at the edge where the behaviour changes, so that is where bugs often hide. Add a normal input too. A fix can accidentally break something that used to work, so retest every case. The second line only prints the number 16 and never calls square(), so it tests nothing.

Common mistakes: sq has no brackets or input, so the function is not called. The test prints the function itself, not the actual result. It should be sq(5). Squaring means multiplying n by itself. With n + n, sq(5) gives 10, so the test would show 10 next to the expected 25 and fail. It should be return n * n;.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 624–634) and word search (Modules 611–634).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line compares the actual output of square(5) with the expected value?
console.log("square(5)");console.log("expected 25, got", square(5));
Matches
console.log("expected 25, got", square);
↓
square is not called. Without brackets and an input like (5), it logs the function itself instead of the result.
Applying
Meanings
Term
console.log("expected 25, got", square(5))
Meaning
Test a function by calling it with chosen inputs, logging the actual result next to the expected value, then recording pass or fail. Retest after any fix.
Practice
function square(n) {
Testing
Gaps
console.log("expected 25, got", (5));
Check Answer
Spots
3}
4console.log("want 25", sq);

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Fox' badge — Cunning Debugger, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line compares the actual output of square(5) with the expected value?
A. console.log("expected 25, got", square(5)); ✓
B. console.log("square(5)");
A good test logs the expected value next to the actual result from calling the function, so you can compare them.
2. square(5) logs "expected 25, got 10". What is the test result?
A. Pass, because the function ran without an error
B. Fail, because the actual value does not match the expected value ✓
A test passes only when the actual output matches the expected output. Running without an error is not enough.
3. Which is the best way to choose the expected value for a test?
A. Work it out by hand before you run the function ✓
B. Copy whatever the function prints and call it expected
If you copy the output as the expected value, the test always passes, even when the function is wrong.
4. Which set of inputs is the better test of a function that checks if a mark is a pass (50 or more)?
A. 80, 81 and 82 only
B. 49, 50 and 51 (boundary values) plus a normal value like 80 ✓
Boundary inputs sit right at the edge where the behaviour changes, so that is where bugs often hide. Add a normal input too.
5. You fix a bug in your function. What should you do next?
A. Run all the tests again, including the ones that passed before ✓
B. Only rerun the test that failed, because the others are fine
A fix can accidentally break something that used to work, so retest every case.

Match the Pairs

console.log("expected 25, got", square(5)); → Calls square with the input 5 and prints the actual result next to the expected value 25 so you can compare them.
console.log("expected 0, got", square(0)); → Tests a boundary input: 0 is an edge case, and the expected result is written beside the actual result.
Test a function with a normal input and log the expected value beside the actual output → console.log("expected 36, got", square(6));
After fixing a bug, run the same test again to check the fix worked → console.log("retest: expected 25, got", square(5));
console.log("expected 25, got", square); → square is not called. Without brackets and an input like (5), it logs the function itself instead of the result.
console.log("expected 25, got 25"); → The actual result is typed in by hand, so the function is never called and the test proves nothing.

Key Term

console.log("expected 25, got", square(5))
Test a function by calling it with chosen inputs, logging the actual result next to the expected value, then recording pass or fail. Retest after any fix.

Fill in the Blanks

1. console.log("expected 25, got", square(5));
Hint: Type the name of the function being tested, which is called with 5.
2. console.log("expected 25, got", square(5));
Hint: Type the test input that should give the expected result shown in the message.
3. console.log("expected 0, got", square(0));
Hint: Type the boundary input that should give the expected result shown in the message.
4. let actual = square(4); if (actual === 16) console.log("pass");
Hint: Type the strict equality operator that compares the actual result with the expected value.
5. function square(n) { return n * n; } console.log("expected 25, got", square(5));
Hint: Type the arithmetic operator that fixes the bug so the retest passes.

Spot the Error

Question 1
A function sq(n) {
B return n * n;
C }
D console.log("want 25", sq);
Line D — sq has no brackets or input, so the function is not called. The test prints the function itself, not the actual result. It should be sq(5).
Question 2
A function sq(n) {
B return n + n;
C }
D let got = sq(5);
E console.log("want 25", got);
Line B — Squaring means multiplying n by itself. With n + n, sq(5) gives 10, so the test would show 10 next to the expected 25 and fail. It should be return n * n;
Question 3
A function sq(n) {return n*n;}
B let got = sq(5);
C if (got = 25) {
D console.log("pass");
E }
Line C — A single = assigns 25 to got instead of comparing, so the test always 'passes'. Use === to compare the actual and expected values: if (got === 25) {
Question 4
A function isAdult(age) {
B return age >= 18;
C }
D let got = isAdult(17);
E console.log("want true", got);
Line D — The test expects true for the boundary input 18. Calling isAdult(17) returns false, so the test does not check the boundary it claims to. It should be isAdult(18).
Question 5
A function sq(n) {return n*n;}
B let got = sq(6);
C console.log("want 36", get);
Line C — The variable is called got, not get. Using get causes a ReferenceError, so no actual value is shown. It should be console.log("want 36", got);

Student with a class code? Join your class →