Problem overview
This version of the shopping cart adds a rule: a cart whose subtotal reaches a threshold ships for free. The program starts from three constants, subtotal (75), shipping_cost (10) and free_shipping_threshold (100). It must print three lines, Subtotal, Shipping and Total, joined into one string called result and printed with a single console.log.
With the starting values, 75 is below 100, so shipping costs 10 and the total is 85:
Subtotal: $75
Shipping: $10
Total: $85
When the cart ships free, the Shipping line is still printed, showing $0. For a subtotal of 120 the three lines are Subtotal: $120, Shipping: $0 and Total: $120. The earlier version already built this kind of receipt; what is new here is that one number, the shipping, depends on a condition.
Approach: decide the shipping first, then build the text
The Total line needs the shipping amount, and the Shipping line needs it too, so the order of work matters: first decide shipping, then compute total, then format the lines. An if/else does the deciding. One branch handles carts at or above the threshold, the other handles the rest.
The comparison is subtotal >= free_shipping_threshold. It uses >=, not >, because a cart exactly on the threshold counts as free. A shortcut such as a ternary would also work, but the lesson uses a plain if/else so each branch has room for more statements later.
Reading the finished program from top to bottom with a subtotal of 75: the condition 75 >= 100 is false, so the else branch runs and shipping becomes 10. The total is 85, and the three lines are built from those three numbers. With a subtotal of 120 the condition is true, shipping becomes 0, and everything after the if/else is identical code that now produces different text. That is the pattern of this lesson: a branch decides a value, and straight-line code uses it.
Step by step
- Declare
shippingwithletand no value. It gets its value in one of two branches, so it cannot be aconstwith an initializer. Declaring it before theifalso keeps it visible afterwards. - Write the
ifwith its condition and close its block right away, so the braces always match. - In the
ifbranch, setshippingto 0. The threshold is met, so the customer pays nothing. - Add the
elsebranch, on the same line as the closing brace of theif. - In the
elsebranch, setshippingtoshipping_cost. - Compute
totalassubtotal + shipping. When shipping is free this equals the subtotal. - Build
line1,line2andline3, each a label followed by a number. The+operator converts the number to text when the other operand is a string. - Join the lines into
resultwith"\n"between them: two newlines for three lines, and none at the end. - Print
resultonce.
The solution in TypeScript
const subtotal: number = 75;
const shipping_cost: number = 10;
const free_shipping_threshold: number = 100;
let shipping: number;
if (subtotal >= free_shipping_threshold) {
shipping = 0;
} else {
shipping = shipping_cost;
}
const total: number = subtotal + shipping;
const line1: string = "Subtotal: $" + subtotal;
const line2: string = "Shipping: $" + shipping;
const line3: string = "Total: $" + total;
const result: string = line1 + "\n" + line2 + "\n" + line3;
console.log(result);
A few details are worth knowing.
let shipping: number;without a value is allowed. Until an assignment runs, the variable holdsundefined. If theelsebranch were missing and the cart were under the threshold,subtotal + shippingwould be75 + undefined, and the program would printTotal: $NaN. The lesson's flags don't make the compiler stop you, so both branches must assign.- The type annotations disappear when the code is compiled to JavaScript. They help the compiler and the reader; the output comes from the JavaScript that remains.
- The dollar sign in
"Subtotal: $"is an ordinary character inside a normal double-quoted string. It only has a special meaning inside template literals, written with backticks, when followed by{. - Numbers are converted with their shortest decimal form. Whole numbers print without a decimal point, so 75 appears as
75, not75.0.
Edge cases and common mistakes
- Using
>instead of>=. With a subtotal of exactly 100 the cart must ship free. The solution printsShipping: $0andTotal: $100for it; with>the customer would be charged 10. - Leaving out the Shipping line when it is free. All three lines are always printed.
- Adding
shipping_costto the total instead ofshipping. Then a free cart would still be charged. - Declaring
shippinginside a branch. Aletorconstdeclared inside the braces of theifexists only there, so the later lines could not see it. - Decimal prices. Numbers are binary doubles, so a subtotal of 89.9 with a shipping cost of 4.2 gives
Total: $94.10000000000001. The lesson's whole-number values avoid this; real prices are usually kept in cents, or rounded withtoFixed(2), before printing.
Testing
Try changing subtotal in the editor above. These results come from the reference solution:
| subtotal | Shipping line | Total line |
|---|---|---|
| 75 | Shipping: $10 | Total: $85 |
| 99 | Shipping: $10 | Total: $109 |
| 100 | Shipping: $0 | Total: $100 |
| 120 | Shipping: $0 | Total: $120 |
