Predict, then run
Show on a big screenRun it first, and you may believe what it printed.
- Predict all three lines first: 2, 1, 0.
- Run it. Line 1 matches: 2.
- Line 2 does not match: 0, not 1.
- Line 3 matches: 0. Only line 2 is wrong.
Predict each purse below. Then see what the program prints.
Loading lab…
The rule
Write down what each line should print, before you run it. Predict from the job, not from the program. The first line where they differ is the one to check.
A ticket costs 8 rupees. You buy one whenever you have at least 8. Three purses hold 20, 8 and 4 rupees. This program counts the tickets.
In the program, n counts the tickets. The box left is the money not yet spent. From the job, 20 rupees buys 2 tickets. 8 rupees buys 1. 4 rupees buys none. So the prediction is 2, 1, 0.
`` function tickets(money) { let n = 0; let left = money; while (left > 8) { n = n + 1; left = left - 8; } return n; } ``
A prediction copied from the code repeats its mistake.
The program prints 2, 0 and 0. Only the second printed line differs.
Here only one printed line is wrong. Sometimes many printed lines are wrong from one fault. Then the later lines only repeat it. Look for the cause of the first difference. Mend that, then run again.
The wrong test is left > 8 on line 4. A purse of exactly 8 should buy a ticket. So it must be left >= 8.
Try the lab to finish this lesson.