Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
This Functions that return true or false (JavaScript) worksheet is part of the ACARA Digital Technologies Years 9–10 — Modular and Object-Oriented Programming (JavaScript) journey. Print it as a booklet?
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right 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 |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
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).function isEven(n) { return n % 2 === 0; }function isValidAge(age) {
return age >= 0 && age <= 120;
}if (isEven(n)) {
console.log("even");
}function isValidAge(age) {
return age >= 13;
}const n = 7;
if (!isEven(n)) console.log("odd");