Transfer practice · drain(initial) must use repeat to subtract 1 at least once, stopping at zero or below. Repair the amount subtracted. Compare drain(0) with a while-based drain: repeat runs its body before checking.
v3 · Activity 1 of 1 · while · repeat · termination · 11 min Runs here
Use a familiar idea in a different task. Read the changed requirement before running.
drain(initial) must use repeat to subtract 1 at least once, stopping at zero or below. Repair the amount subtracted. Compare drain(0) with a while-based drain: repeat runs its body before checking.
Interface: chargeTo(initial:number,target:number,increment:number) -> number
| Input | Expected |
|---|---|
Positive charge3 | 0 |
Zero begins with an iteration0 | -1 |
local charge = 0
while charge < 5 do
charge = charge + 1
end
return chargeEach iteration increases charge. After five updates, charge < 5 is false and the loop stops.
New syntax: while ... do, repeat ... until
while checks its condition before each iteration. The body must change state so the condition eventually becomes false.
repeat checks its until condition after the body, so it runs at least once. That is useful only when one attempt is appropriate even at the initial boundary.
This exercise accepts positive whole-number increments. Invalid increments must return the initial charge immediately. The browser and server enforce external limits too, but your program should still have a valid termination argument.
Select a word or symbol in this authored example. Your editor draft stays unchanged.
chargeToinitial, target, increment
increment 0 initial
charge initial
charge increment target
charge charge increment
charge
chargeToThis is a private experiment copy. It does not replace your answer or complete the activity.
These inputs apply when your scratchpad returns the function described by the activity interface.
Your output will appear here.
Replace this code with the starter? Download your draft to keep a copy.
Your output will appear here.
Sign in with RoCreators to save private progress across devices. Community verification is separate.