Using if and else inside a function

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

Using if and else inside a function

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does gradeFor(85) return? function gradeFor(score) { if (score >= 80) { return "A"; } return "B"; }
A "B"
B "A"
2. What does gradeFor(60) return? function gradeFor(score) { if (score >= 80) { return "A"; } return "B"; }
A "B"
B "A"
3. Which function body returns a different value from each branch?
A if (score >= 50) { console.log("Pass"); } else { console.log("Fail"); }
B if (score >= 50) { return "Pass"; } else { return "Fail"; }
4. What happens after a return statement runs inside an if branch?
A The function stops and gives back that value
B The function carries on and runs the next lines too
5. Why is a default return at the end of gradeFor useful?
A It covers every score that no if branch matched
B It makes the if statements run twice

Match the Pairs

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

Left columnRight column
1if (score >= 80) { return "A"; } AA default return at the end of the function that covers every score the earlier branches did not catch.
2return "C"; BIf the score is 80 or more, the function returns "A" straight away and stops running.
3A function can return a different value from each branch of an if and else. Cfunction gradeFor(score) { if (score >= 50) { return "Pass"; } else { return "Fail"; } }
4Put a default return after the if statements so every other case still gets a value. DThere is no default return. A score below 80 skips the if and the function returns undefined.
5function gradeFor(score) { if (score >= 80) { console.log("A"); } } EThe branch uses console.log instead of return, so the function gives nothing back. It returns undefined.
6function gradeFor(score) { if (score >= 80) { return "A"; } } Fif (score >= 80) { return "A"; } if (score >= 65) { return "B"; } return "C";

Definitions

if (score >= 80) { return "A"; }

Fill in the Blanks

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

1.
function gradeFor(score) { __ (score >= 50) { return "Pass"; } return "Fail"; }
2.
function gradeFor(score) { if (score __ 80) { return "A"; } return "B"; }
3.
function gradeFor(score) { if (score >= 80) { return "A"; } ______ "C"; }
4.
function gradeFor(score) { if (score >= 50) { return "Pass"; } ____ { return "Fail"; } }
5.
function gradeFor(score) { if (_____ >= 80) { return "A"; } return "C"; }

Spot the Error

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

Question 1
A function gradeFor(score) {
B if (score >= 80 {
C return "A";
D }
E return "B";
F }
Question 2
A func gradeFor(score) {
B if (score >= 80) {
C return "A";
D }
E return "C";
F }
Question 3
A function isPass(mark {
B if (mark >= 50) {
C return true;
D }
E return false;
F }
Question 4
A function gradeFor(s) {
B if (s >= 50) { return "P"; }
C els { return "F"; }
D }
Question 5
A function describe(n) {
B if (n = 0) {
C return "zero";
D }
E return "not zero";
F }

Answer Key

Using if and else inside a function
Section A: Quick Questions
Q1
B — "A"
Q2
A — "B"
Q3
B — if (score >= 50) { return "Pass"; } else { return "Fail"; }
Q4
A — The function stops and gives back that value
Q5
A — It covers every score that no if branch matched
Section B: Match the Pairs
Q1
if (score >= 80) { return "A"; } → If the score is 80 or more, the function returns "A" straight away and stops running.
Q2
return "C"; → A default return at the end of the function that covers every score the earlier branches did not catch.
Q3
A function can return a different value from each branch of an if and else. → function gradeFor(score) { if (score >= 50) { return "Pass"; } else { return "Fail"; } }
Q4
Put a default return after the if statements so every other case still gets a value. → if (score >= 80) { return "A"; } if (score >= 65) { return "B"; } return "C";
Q5
function gradeFor(score) { if (score >= 80) { console.log("A"); } } → The branch uses console.log instead of return, so the function gives nothing back. It returns undefined.
Q6
function gradeFor(score) { if (score >= 80) { return "A"; } } → There is no default return. A score below 80 skips the if and the function returns undefined.
Section C: Definitions
Q1
if (score >= 80) { return "A"; } — A function can use if and else to make a decision, then return a different value from each branch. A default return at the end covers every other case.
Section D: Fill in the Blanks
Q1
function gradeFor(score) { if (score >= 50) { return "Pass"; } return "Fail"; }
Q2
function gradeFor(score) { if (score >= 80) { return "A"; } return "B"; }
Q3
function gradeFor(score) { if (score >= 80) { return "A"; } return "C"; }
Q4
function gradeFor(score) { if (score >= 50) { return "Pass"; } else { return "Fail"; } }
Q5
function gradeFor(score) { if (score >= 80) { return "A"; } return "C"; }
Section E: Spot the Error
Q1
B — The closing bracket after the condition is missing. It should be if (score >= 80) {.
Q2
A — JavaScript declares a function with the keyword function, not func. It should be function gradeFor(score) {.
Q3
A — The bracket after the parameter is missing. It should be function isPass(mark) {.
Q4
C — The keyword is misspelt. It should be else, so the code runs when the if condition is false.
Q5
B — A single = assigns a value instead of comparing. The condition should use ===: if (n === 0) {.