Methods calling other methods

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

Methods calling other methods

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Inside the Student class, how do you call the isPassing method from another method?
A this.isPassing()
B isPassing()
2. Which line is the correct way to call the method getTotal from inside another method of the same class?
A let t = getTotal();
B let t = this.getTotal();
3. A method contains the line: return average(); and average is a method in the same class. What happens when it runs?
A It calls the average method correctly
B ReferenceError: average is not defined
4. Which is a good example of a small single-job method?
A isPassing() returns true if the mark is 50 or more
B doEverything() calculates marks, prints reports and checks passes
5. Which method body correctly uses another method? report() { ... }
A if (this.isPassing()) { console.log('Pass'); }
B if (this.isPassing) { console.log('Pass'); }

Match the Pairs

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

Left columnRight column
1if (this.isPassing()) { AThe brackets are missing, so the method is not called. this.isPassing is just the function itself, which is always truthy. It should be this.isPassing().
2return this.getTotal() / 3; BGets the total from another method of the same object, then divides it by 3 to give an average
3A small single-job method checks one thing, so other methods can call it with this. CThe call is missing this. so JavaScript looks for a normal function called isPassing and throws a ReferenceError. It should be this.isPassing().
4One method can call another method of the same class by writing this. before the method name. DisPassing() { return this.mark >= 50; }
5report() { return isPassing(); } Ereport() { return this.getGrade(); }
6if (this.isPassing) { return 'Pass'; } FCalls the isPassing method on the same object and uses its true or false result in the if condition

Definitions

this.methodName()

Fill in the Blanks

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

1.
isPassing() { return this.mark >= 50; } report() { return this._________(); }
2.
isPassing() { return this.mark >= 50; } status() { return ____.isPassing(); }
3.
total() { return this.a + this.b; } average() { return this._____() / 2; }
4.
addBonus(n) { this.points += n; } win() { this.________(10); }
5.
status() { ______ this.isPassing() ? "Yes" : "No"; }

Spot the Error

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

Question 1
A report() {
B if (isPassing()) {
C return 'Pass';
D }
E }
Question 2
A ok() { return pass(); }
B pass() {
C return this.mark >= 50;
D }
Question 3
A summary() {
B let n = this.name;
C let p = this.isPassing;
D return n + ' ' + p;
E }
Question 4
A report() {
B let n = this.name;
C let m = this.mark;
D let p = this.ispassing();
E return n + m + p;
F }
Question 5
A summary() {
B let n = this.name;
C let m = this.mark;
D let g = Student.grade();
E return n + m + g;
F }

Answer Key

Methods calling other methods
Section A: Quick Questions
Q1
A — this.isPassing()
Q2
B — let t = this.getTotal();
Q3
B — ReferenceError: average is not defined
Q4
A — isPassing() returns true if the mark is 50 or more
Q5
A — if (this.isPassing()) { console.log('Pass'); }
Section B: Match the Pairs
Q1
if (this.isPassing()) { → Calls the isPassing method on the same object and uses its true or false result in the if condition
Q2
return this.getTotal() / 3; → Gets the total from another method of the same object, then divides it by 3 to give an average
Q3
A small single-job method checks one thing, so other methods can call it with this. → isPassing() { return this.mark >= 50; }
Q4
One method can call another method of the same class by writing this. before the method name. → report() { return this.getGrade(); }
Q5
report() { return isPassing(); } → The call is missing this. so JavaScript looks for a normal function called isPassing and throws a ReferenceError. It should be this.isPassing().
Q6
if (this.isPassing) { return 'Pass'; } → The brackets are missing, so the method is not called. this.isPassing is just the function itself, which is always truthy. It should be this.isPassing().
Section C: Definitions
Q1
this.methodName() — Inside a class, a method calls another method of the same object by writing this. before its name, such as this.isPassing(). Forgetting this. causes a ReferenceError.
Section D: Fill in the Blanks
Q1
isPassing() { return this.mark >= 50; } report() { return this.isPassing(); }
Q2
isPassing() { return this.mark >= 50; } status() { return this.isPassing(); }
Q3
total() { return this.a + this.b; } average() { return this.total() / 2; }
Q4
addBonus(n) { this.points += n; } win() { this.addBonus(10); }
Q5
status() { return this.isPassing() ? "Yes" : "No"; }
Section E: Spot the Error
Q1
B — isPassing() is a method of the object, so calling it without this. causes a ReferenceError. It should be this.isPassing().
Q2
A — One method calling another must use this. before the name. Plain pass() looks for a separate function and causes a ReferenceError. It should be this.pass().
Q3
C — Without the brackets the method is not called. p would hold the function itself instead of true or false. It should be this.isPassing().
Q4
D — JavaScript is case sensitive. The method is called isPassing, so this.ispassing() is not a function and causes a TypeError. It should be this.isPassing().
Q5
D — Student is the class, but grade() belongs to each object made from it. Inside a method, call it on the current object with this.grade().