STRATEGY / PRACTICE
One checkout.
More ways to deliver.
You met Strategy through sorting. Can you use it to handle shipping?
YOUR BRIEF
Standard works. Express is next.
Your checkout already quotes Standard shipping. Now customers need Express—and the ability to switch delivery methods. Assemble a design that lets Checkout request a fee without owning each delivery rule.
Fictional teaching prices. Fixed input: 2 kg. Each quote accepts weightKg, returns a nonnegative fee in integer cents, and leaves the input unchanged. This exercise uses whole kilograms.
The working starting point
function standardOnlyCheckout(weightKg: number): number {
return 500 + 100 * weightKg;
}
// 2 kg → 700 cents ($7.00)New Express rule: 1000 + 200 × weightKg → 1400 cents ($14.00).
01 / ASSEMBLE
Give shipping one shared job.
Choose three pieces. Then run the checkout to test your design.
Your assembled code
TypeScript preview. Highlighting follows the selected execution. Examples use the fixed 2 kg package.
The contract
// Choose a shared contract above.The implementations
// Choose how Standard and Express expose their work.Checkout · the context
// Choose Checkout's calculation above.Caller · delivery selection
// Caller supplies the selected shipping method here.02 / RUN & OBSERVE
Same package. Your design.
Your calculation goes hereWho should own the delivery calculation?500 + 100 × kgNot called yet1000 + 200 × kgNot called yetReady when you are. Assemble the code and run a quote.
Your price and design feedback will appear here.
A deterministic local simulation of your choices. Animation slows down the explanation, not the calculation. No real shipping service is called.
- ○ Standard · $7.00 — run with delegation
- ○ Express · $14.00 — run with delegation
03 / TRANSFER THE IDEA
Now add free pickup.
PickupShipping returns 0 cents for the same package. Which parts need to change?
First verify Standard and Express with a delegating Checkout. This challenge opens when both pass.
Compare with a complete solution
No timer and no attempt limit. Try another assembly, use a hint, or reveal the answer whenever it helps.