Watch a Variable Change
Scenario
A study room begins with two available seats.
- 1Set freeSeats to 2
- 2One student books a seat
- 3That student cancels
Try it yourself
Predict the value of freeSeats after each event.
| Case | State after the step |
|---|---|
| Set freeSeats to 2 | — |
| One student books a seat | — |
| That student cancels | — |
Reveal the explanation
- Set freeSeats to 2
The current value is 2.
- One student books a seat
Subtract one. The current value is 1.
- That student cancels
Add one. The current value is 2.
A trace records the value after each update. Starting at 2, booking twice would leave 0.
Your turn
Start with two seats. Book one, book one more, then cancel one. Record the value after each event.
Common mistake Reading the old value after an assignment.
Check your work
The trace shows 2, 1, 0, 1 in that order.
This browser could not save your progress. Changes last until you leave the page.