JavaScript From Zero · 22 min · 180 XP
Checkpoint: checkout calculator
Turn messy form-like text into a trustworthy checkout total using conversion, functions and guard clauses.
This checkpoint starts from an empty editor: you write both functions yourself. Before you type any code, list the inputs that should give null, and open the hint only after your first run.
Everything here comes from the lessons so far, used together. Form fields arrive as text, so a checkout does three jobs, in this order: check the input and turn it into numbers, do the maths, then format the answer for a person. Keep them apart. Format too early and you're doing maths on text; convert without checking and an empty field quietly becomes 0, which looks like a real answer.
A plan that works: trim both inputs and return null for an empty one. Convert with Number, then turn away anything unusable: a price that isn't finite or is negative, a quantity that isn't a whole number. Work out the total, take off the member discount, and round to the cent once, at the end. Only the label turns the number into text.
Loading your workspace…