Idea 21of 23Builds onChange one number, see what changes
Reuse code for a new job
How can old code solve a new problem?
Today you'll discover
- which lines hold the story's numbers
- how little needs to change
- how to check the new code
Think firstSuzie's code: $15, then $5 a week for 4 weeks
Jimmy has $55 and saves $5 a week for 4 weeks. Which line changes for him?
Decide first, then press Next. Match each number in the story to a line of code.
Suzie's code on the left: SET 15, REPEAT 4 TIMES, ADD 5, END, OUTPUT, giving $35. Jimmy's code on the right: the same, except SET 55, which is framed as changed, giving $75. The last step shows the code changed for a candle: SET 24, REPEAT 5 TIMES, SUBTRACT 3.
See the whole ideathe big idea and the key word, in one place
The big idea
Reuse code by changing only what is different in the new situation.
Where the story's numbers go
SET holds the start, REPEAT holds how many times, and ADD or SUBTRACT holds the change each time.
- SETstart
- REPEAThow many
- ADDchange
The candleSuzie's saving loop becomes a burning candle: SET 24, REPEAT 5 TIMES, SUBTRACT 3. It outputs 9.
Key word
- reuse
- to use code again for a new situation, changing only the parts that are different
Ready to use it?
- Change existing code so it solves a new problem.