Functions that return true or false (JavaScript)

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

Functions that return true or false (JavaScript)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which function returns a Boolean?
A function double(n) { return n * 2; }
B function isEven(n) { return n % 2 === 0; }
2. Which is the correct way to use a Boolean function in an if statement?
A if (isValidAge(age)) { ... }
B if (isValidAge(age) === true === ) { ... }
3. isEven(4) returns true. What happens in: if (isEven(4)) { console.log('Even'); }
A Nothing is printed
B 'Even' is printed
4. isValidAge(200) returns false. What happens in: if (isValidAge(200)) { console.log('OK'); }
A Nothing is printed
B 'OK' is printed
5. Which function would work as an if condition?
A function isValidAge(age) { return age >= 0 && age <= 120; }
B function isValidAge(age) { console.log(age >= 0 && age <= 120); }

Match the Pairs

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

Left columnRight column
1function isEven(n) { return n % 2 === 0; } Afunction isValidAge(age) { return age >= 0 && age <= 120; }
2if (isValidAge(age)) { console.log('OK'); } BThe function is not called because the brackets and argument are missing, so the condition is the function itself, which is always truthy
3A function can return the result of a comparison, which is already true or false Cif (isEven(n)) { console.log('even'); }
4A Boolean-returning function call can be used by itself as an if condition DThe function never uses return, so it gives back undefined instead of true or false and the if block never runs
5function isEven(n) { n % 2 === 0; } ECalls isValidAge and runs the block only if it returns true
6if (isValidAge) { console.log('OK'); } FA function that returns true when n is even and false when it is odd

Definitions

if (isValidAge(age))

Fill in the Blanks

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

1.
function isEven(n) { return n _ 2 === 0; }
2.
function isValidAge(age) { return age >= 0 __ age <= 120; }
3.
__ (isEven(n)) { console.log("even"); }
4.
function isValidAge(age) { ______ age >= 13; }
5.
const n = 7; if (_isEven(n)) console.log("odd");

Spot the Error

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

Question 1
A function isEven(n) {
B return n / 2 === 0;
C }
D if (isEven(6)) {
E console.log("Even");
F }
Question 2
A function isValidAge(age) {
B return age > 0 || age < 120;
C }
D if (isValidAge(15)) {
E console.log("Valid");
F }
Question 3
A function isValidAge() {
B return age >= 18;
C }
D if (isValidAge(20)) {
E console.log("Allowed");
F }
Question 4
A function isValidAge(age) {
B return age >= 13;
C }
D if (isValidAge) {
E console.log("Valid");
F }
Question 5
A const isValidAge = (age) -> {
B return age >= 16;
C }
D if (isValidAge(17)) {
E console.log("Valid");
F }

Answer Key

Functions that return true or false (JavaScript)
Section A: Quick Questions
Q1
B — function isEven(n) { return n % 2 === 0; }
Q2
A — if (isValidAge(age)) { ... }
Q3
B — 'Even' is printed
Q4
A — Nothing is printed
Q5
A — function isValidAge(age) { return age >= 0 && age <= 120; }
Section B: Match the Pairs
Q1
function isEven(n) { return n % 2 === 0; } → A function that returns true when n is even and false when it is odd
Q2
if (isValidAge(age)) { console.log('OK'); } → Calls isValidAge and runs the block only if it returns true
Q3
A function can return the result of a comparison, which is already true or false → function isValidAge(age) { return age >= 0 && age <= 120; }
Q4
A Boolean-returning function call can be used by itself as an if condition → if (isEven(n)) { console.log('even'); }
Q5
function isEven(n) { n % 2 === 0; } → The function never uses return, so it gives back undefined instead of true or false and the if block never runs
Q6
if (isValidAge) { console.log('OK'); } → The function is not called because the brackets and argument are missing, so the condition is the function itself, which is always truthy
Section C: Definitions
Q1
if (isValidAge(age)) — A function that returns true or false can be called right inside an if condition. The if block runs only when the function returns true, like isEven(n) or isValidAge(age).
Section D: Fill in the Blanks
Q1
function isEven(n) { return n % 2 === 0; }
Q2
function isValidAge(age) { return age >= 0 && age <= 120; }
Q3
if (isEven(n)) { console.log("even"); }
Q4
function isValidAge(age) { return age >= 13; }
Q5
const n = 7; if (!isEven(n)) console.log("odd");
Section E: Spot the Error
Q1
B — Dividing by 2 does not test for an even number. Use the remainder operator: return n % 2 === 0; so the function returns true only for even numbers.
Q2
B — With || the function returns true for almost any number, so invalid ages pass the if check. Both conditions must hold, so use &&: return age > 0 && age < 120;
Q3
A — The function needs a parameter to receive the age. Without it, age is not defined inside the function. The line should be function isValidAge(age) {
Q4
D — This names the function but never calls it. A function name on its own is always truthy. Call it with a value: if (isValidAge(14)) {
Q5
A — JavaScript arrow functions use => and not ->. The line should be const isValidAge = (age) => {