Methods that change an object's properties

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

Methods that change an object's properties

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which method line changes the object's own balance?
A this.balance += amount;
B balance += amount;
2. After account.deposit(50) runs on an account with balance 100, what is account.balance?
A 100
B 150
3. If you call account.deposit(50) twice on a balance of 100, what is the balance?
A 200
B 150
4. Which method checks the amount before it changes the balance?
A if (amount > 0) { this.balance += amount; }
B this.balance += amount; if (amount > 0) { }
5. A withdraw method has this line: this.balance -= amount; What does calling it do?
A It only prints a smaller number and leaves balance alone
B It lowers the object's balance each time it is called

Match the Pairs

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

Left columnRight column
1this.balance += amount; AThe + operator only works out a total and throws it away. It never stores the result, so this.balance does not change.
2if (amount > 0) { this.balance += amount; } Bacc.deposit(50); acc.deposit(50); // balance is now 100 if it started at 0
3A method changes the object's state by updating a property with this CAdds the amount to the object's own balance property, so the object's state changes
4Calling a changing method twice applies the change twice, so the state builds up DChecks the amount is valid before changing the property, so bad values leave the state unchanged
5deposit(amount) { balance += amount; } Edeposit(amount) { this.balance += amount; }
6deposit(amount) { this.balance + amount; } Fbalance has no this. in front of it, so the method does not update the object's property. It looks for a plain variable and causes an error.

Definitions

this.balance += amount

Fill in the Blanks

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

1.
deposit(amount) { this.balance __ amount; }
2.
if (amount __ this.balance) { this.balance -= amount; }
3.
withdraw(amount) { ____.balance -= amount; }
4.
account.deposit(50); account.deposit(50); console.log(account._______);
5.
__ (amount > 0) { this.balance += amount; }

Spot the Error

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

Question 1
A deposit(amount {
B this.balance += amount;
C }
Question 2
A class Account {
B deposit(amount) {
C balance += amount;
D }
E }
Question 3
A const acc = new Account(100);
B acc.deposit(50);
C acc.deposit(50);
D console.log(acc.balance());
Question 4
A function withdraw(amt) {
B if (this.balance >= amt) {
C this.balance -= amt;
D }
E }
Question 5
A deposit(amount) {
B if (amount <= 0) return;
C this.balance += amount;
D this.deposits + 1;
E }

Answer Key

Methods that change an object's properties
Section A: Quick Questions
Q1
A — this.balance += amount;
Q2
B — 150
Q3
A — 200
Q4
A — if (amount > 0) { this.balance += amount; }
Q5
B — It lowers the object's balance each time it is called
Section B: Match the Pairs
Q1
this.balance += amount; → Adds the amount to the object's own balance property, so the object's state changes
Q2
if (amount > 0) { this.balance += amount; } → Checks the amount is valid before changing the property, so bad values leave the state unchanged
Q3
A method changes the object's state by updating a property with this → deposit(amount) { this.balance += amount; }
Q4
Calling a changing method twice applies the change twice, so the state builds up → acc.deposit(50); acc.deposit(50); // balance is now 100 if it started at 0
Q5
deposit(amount) { balance += amount; } → balance has no this. in front of it, so the method does not update the object's property. It looks for a plain variable and causes an error.
Q6
deposit(amount) { this.balance + amount; } → The + operator only works out a total and throws it away. It never stores the result, so this.balance does not change.
Section C: Definitions
Q1
this.balance += amount — A method line that updates the object's own property, so its state changes each time the method is called. Calling deposit(50) twice adds 50 twice.
Section D: Fill in the Blanks
Q1
deposit(amount) { this.balance += amount; }
Q2
if (amount <= this.balance) { this.balance -= amount; }
Q3
withdraw(amount) { this.balance -= amount; }
Q4
account.deposit(50); account.deposit(50); console.log(account.balance);
Q5
if (amount > 0) { this.balance += amount; }
Section E: Spot the Error
Q1
A — The closing bracket is missing from the parameter list. It should be deposit(amount) {.
Q2
C — Without this., JavaScript looks for a variable called balance, not the object's property. It should be this.balance += amount;.
Q3
D — balance is a property, not a method, so it must not be called with (). It should be console.log(acc.balance); and prints 200.
Q4
A — Methods inside a class are written without the function keyword. It should be withdraw(amt) {.
Q5
D — this.deposits + 1 works out a value but never stores it, so the property does not change. It should be this.deposits += 1;.