Transfer practice · shipping(total) returns 0 at totals of 30 or above and 4 below 30. Include the exact free-shipping boundary.
v3 · Activity 1 of 1 · return-values · early-return · 9 min Runs here
Use a familiar idea in a different task. Read the changed requirement before running.
shipping(total) returns 0 at totals of 30 or above and 4 below 30. Include the exact free-shipping boundary.
Interface: price(quantity, unitPrice) -> number
| Input | Expected |
|---|---|
Below threshold29 | 4 |
Boundary30 | 0 |
Above42 | 0 |
local function points(count)
if count < 0 then return 0 end
return count * 5
end
local total = points(3) + 2
print(total)
return pointspoints returns 15, allowing the caller to add 2. Merely printing 15 would not supply that value to +.
New syntax: early return, negative numbers
print exposes a value to a person; return gives it to the caller. A function without a returned value supplies nil.
return also stops the current function call. A guard at the top can reject a bad input before the main calculation.
Our rule treats a negative quantity as invalid and returns 0. This is an exercise policy, not a complete store security design.
Select a word or symbol in this authored example. Your editor draft stays unchanged.
pricequantity, unitPrice
quantity 0
0
quantity unitPrice
priceThis 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.