Methods with parameters and return values (JavaScript)

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

This Methods with parameters and return values (JavaScript) worksheet is part of the ACARA Digital Technologies Years 9–10 — Modular and Object-Oriented Programming (JavaScript) journey. Print it as a booklet?

Methods with parameters and return values (JavaScript)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which deposit method adds the amount AND gives the new balance back to the caller?
A deposit(amount) { this.balance += amount; }
B deposit(amount) { this.balance += amount; return this.balance; }
2. Which getArea() method correctly returns the area of a Rectangle object?
A getArea() { return this.width * this.height; }
B getArea() { return width * height; }
3. Which line calls deposit with 50 and saves the value it returns?
A const newBalance = account.deposit(50);
B const newBalance = deposit(50);
4. Which method returns a string such as 'Ava has 120 points' using a template literal?
A describe() { return ${this.name} has ${this.balance} points; }
B describe() { return '${this.name} has ${this.balance} points'; }
5. Where does the parameter amount in deposit(amount) get its value?
A It is a property that every object stores automatically
B From the argument passed in when the method is called, e.g. deposit(50)

Match the Pairs

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

Left columnRight column
1deposit(amount) { this.balance += amount; return this.balance; } Adescribe() { return `${this.name} has $${this.balance}`; }
2getArea() { return this.width * this.height; } BThe properties are missing this. so the method looks for a variable called balance instead of the object's property. It should be this.balance.
3A method can take a parameter and use it with a property to work out a value to return. CThe method never uses return, so the calculated area is thrown away and the caller gets undefined. It should be return this.width * this.height;
4A method can return a string built with a template literal using the object's properties. DA method with no parameters that returns a number worked out from the object's width and height properties.
5deposit(amount) { balance += amount; return balance; } EA method that takes an amount, adds it to the object's balance and returns the new balance.
6getArea() { this.width * this.height; } FaddBonus(extra) { return this.points + extra; }

Definitions

deposit(amount) { this.balance += amount; return this.balance; }

Fill in the Blanks

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

1.
deposit(amount) { this.balance += amount; ______ this.balance; }
2.
deposit(amount) { this.balance __ amount; return this.balance; }
3.
getArea() { return this.width _ this.height; }
4.
getInfo() { return `${____.name} has ${this.points}`; }
5.
const acc = new Account("Sam", 50); const newBal = acc._______(20); console.log(newBal);

Spot the Error

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

Question 1
A class Account {
B deposit(amount) {
C balance += amount;
D return this.balance;
E }
F }
Question 2
A getArea {
B return this.side ** 2;
C }
Question 3
A greet() {
B return 'Hi ${this.name}!';
C }
Question 4
A const acc = new Account(50);
B const t = acc.deposit;
C console.log(t);
Question 5
A class Wallet {
B spend(amount) {
C this.cash -= amount;
D return amount;
E }
F }

Answer Key

Methods with parameters and return values (JavaScript)
Section A: Quick Questions
Q1
B — deposit(amount) { this.balance += amount; return this.balance; }
Q2
A — getArea() { return this.width * this.height; }
Q3
A — const newBalance = account.deposit(50);
Q4
A — describe() { return ${this.name} has ${this.balance} points; }
Q5
B — From the argument passed in when the method is called, e.g. deposit(50)
Section B: Match the Pairs
Q1
deposit(amount) { this.balance += amount; return this.balance; } → A method that takes an amount, adds it to the object's balance and returns the new balance.
Q2
getArea() { return this.width * this.height; } → A method with no parameters that returns a number worked out from the object's width and height properties.
Q3
A method can take a parameter and use it with a property to work out a value to return. → addBonus(extra) { return this.points + extra; }
Q4
A method can return a string built with a template literal using the object's properties. → describe() { return `${this.name} has $${this.balance}`; }
Q5
deposit(amount) { balance += amount; return balance; } → The properties are missing this. so the method looks for a variable called balance instead of the object's property. It should be this.balance.
Q6
getArea() { this.width * this.height; } → The method never uses return, so the calculated area is thrown away and the caller gets undefined. It should be return this.width * this.height;
Section C: Definitions
Q1
deposit(amount) { this.balance += amount; return this.balance; } — A class method takes a parameter (amount) and uses return to give back a value worked out from the object's properties, such as the new balance.
Section D: Fill in the Blanks
Q1
deposit(amount) { this.balance += amount; return this.balance; }
Q2
deposit(amount) { this.balance += amount; return this.balance; }
Q3
getArea() { return this.width * this.height; }
Q4
getInfo() { return `${this.name} has ${this.points}`; }
Q5
const acc = new Account("Sam", 50); const newBal = acc.deposit(20); console.log(newBal);
Section E: Spot the Error
Q1
C — Inside a method, the object's property must be reached with this. Without this, balance is treated as a separate variable and causes a ReferenceError. It should be this.balance += amount;
Q2
A — A method needs parentheses after its name, even when it takes no parameters. It should be getArea() {
Q3
B — Template literals must use backticks. With single quotes, ${this.name} is not replaced and the text is returned as-is. It should be return `Hi ${this.name}!`;
Q4
B — Without parentheses the method is never called, so t holds the function instead of the returned balance. It should be const t = acc.deposit(20);
Q5
D — This returns the parameter that was passed in, not the new value worked out from the object's property. It should be return this.cash;