Functions that return true or false

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

Functions that return true or false

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which function returns a boolean?
A function isEven(n) { return n % 2; }
B function isEven(n) { return n % 2 === 0; }
2. Which if statement uses a boolean function correctly as its condition?
A if (isAdult(age)) {
B if (isAdult(age) === true {
3. What does isAdult(20) return if the function is: function isAdult(age) { return age >= 18; }
A true
B 20
4. Which function returns a comparison result directly?
A function isPassing(mark) { return mark >= 50; }
B function isPassing(mark) { mark >= 50; }
5. Which is the better way to write this function?
A if (age >= 18) { return true; } else { return false; }
B return age >= 18;

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; } ARuns the code inside only when isAdult(age) gives back true
2if (isAdult(age)) { console.log("Welcome"); } BThe function never uses return, so the comparison result is lost and the call gives back undefined instead of true or false. Fix: return age >= 18;
3A function can return a comparison directly, because the comparison already gives true or false CThe function is not called because the brackets and argument are missing. isEven alone is the function itself, not true or false. Fix: if (isEven(n))
4The call to a boolean function can be used on its own as the condition of an if Dfunction isAdult(age) { return age >= 18; }
5function isAdult(age) { age >= 18; } Eif (isEven(n)) { console.log("Even"); }
6if (isEven) { console.log("Even"); } FA function that returns true if n is even and false if it is odd

Definitions

if (isAdult(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 isAdult(age) { return age __ 18; }
3.
if (_______(age)) { console.log("You can vote!"); }
4.
function isEven(n) { ______ n % 2 === 0; }
5.
if (_isEven(n)) { console.log("Odd number"); }

Spot the Error

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

Question 1
A function isEven(n) {
B let r = n % 2;
C return r = 0;
D }
Question 2
A function isAdult(age) {
B return age => 18;
C }
D if (isAdult(20)) {
E console.log('Adult');
F }
Question 3
A function isAdult(age) {
B return age >= 18;
C }
D if (isAdult) {
E console.log('Adult');
F }
Question 4
A function isEven(n {
B return n % 2 === 0;
C }
D if (isEven(7)) {
E console.log('Even');
F }
Question 5
A function isAdult(age) {
B if (age >= 18) {
C return True;
D }
E return false;
F }

Answer Key

Functions that return true or false
Section A: Quick Questions
Q1
B — function isEven(n) { return n % 2 === 0; }
Q2
A — if (isAdult(age)) {
Q3
A — true
Q4
A — function isPassing(mark) { return mark >= 50; }
Q5
B — return age >= 18;
Section B: Match the Pairs
Q1
function isEven(n) { return n % 2 === 0; } → A function that returns true if n is even and false if it is odd
Q2
if (isAdult(age)) { console.log("Welcome"); } → Runs the code inside only when isAdult(age) gives back true
Q3
A function can return a comparison directly, because the comparison already gives true or false → function isAdult(age) { return age >= 18; }
Q4
The call to a boolean function can be used on its own as the condition of an if → if (isEven(n)) { console.log("Even"); }
Q5
function isAdult(age) { age >= 18; } → The function never uses return, so the comparison result is lost and the call gives back undefined instead of true or false. Fix: return age >= 18;
Q6
if (isEven) { console.log("Even"); } → The function is not called because the brackets and argument are missing. isEven alone is the function itself, not true or false. Fix: if (isEven(n))
Section C: Definitions
Q1
if (isAdult(age)) — Uses a function that returns true or false directly as the if condition, so the code inside runs only when the call isAdult(age) gives back true.
Section D: Fill in the Blanks
Q1
function isEven(n) { return n % 2 === 0; }
Q2
function isAdult(age) { return age >= 18; }
Q3
if (isAdult(age)) { console.log("You can vote!"); }
Q4
function isEven(n) { return n % 2 === 0; }
Q5
if (!isEven(n)) { console.log("Odd number"); }
Section E: Spot the Error
Q1
C — A single = assigns instead of comparing, so the function returns 0 and never true or false. A comparison needs ===: return r === 0;
Q2
B — => is not a comparison operator, so this line is a syntax error. The 'greater than or equal to' operator is >=, so it should be return age >= 18;
Q3
D — Without the brackets and argument, isAdult is just the function itself, not a call, so the condition is always truthy. It should call the function: if (isAdult(20)) {
Q4
A — The closing bracket after the parameter list is missing. It should be function isEven(n) {
Q5
C — JavaScript booleans are lowercase. True is not defined and causes a ReferenceError. It should be return true;