Reasoning Checkpoint
A design challenge, worked through in writing — no auto-grading, just a real attempt.
The Challenge
Samantha wants one more discount before festival season: an early-bird price. Anyone who buys more than 30 days before the event gets 15% off.
John asks Anna to write the complete pricing function from scratch, with all the rules, so the whole team can see it in one place. Write it in JavaScript and test it in your browser console.
What Your Function Needs
- A function called
calculateTotal(cart, isStudent, daysBeforeEvent)that returns the total in cents (a whole number). - Add up the price of every ticket in the cart with a loop — it must work for any number of tickets, including 0.
- Apply only one discount — the best one the buyer qualifies for: student 20% off, then early-bird 15% off (more than 30 days before), then group 10% off (5 or more tickets). Otherwise, full price.
- Use named constants for every number in the rules — no magic numbers.
- Throw an error if a ticket has no valid price or
daysBeforeEventis negative. - Test it with at least these cases: 4 tickets 10 days before; 5 tickets 10 days before; 2 tickets 45 days before; a student with 6 tickets; an empty cart.
Stuck? A Few Hints
- Order your
if / else ifchain from the best discount to the smallest. - "More than 30 days" is
daysBeforeEvent > 30, not>=— read the rule carefully, like BUG-142 taught. - A cart for testing can be as simple as
[{ priceCents: 2000 }, { priceCents: 2000 }].
Before You Move On
If your function gave the same five results, you've used everything in this Act: variables and types, operators, conditionals, a loop, a function with parameters and a return value, error handling and clean naming. That's the core of programming in every language — the rest is practice.
