Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
This Functions that return true or false worksheet is part of the ACARA Digital Technologies Years 7–8 — Programming with Control Structures and Functions (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; } | 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 |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
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.function isEven(n) {
return n % 2 === 0;
}function isAdult(age) {
return age >= 18;
}if (isAdult(age)) {
console.log("You can vote!");
}function isEven(n) {
return n % 2 === 0;
}if (!isEven(n)) {
console.log("Odd number");
}