Start learning free

LESSON 191 OF 289 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V3 in TypeScript: Free Shipping

Practice Read the full explanation

Use an if/else to decide whether to charge shipping based on a free-shipping threshold.

You start with:

const subtotal: number = 75;
const shipping_cost: number = 10;
const free_shipping_threshold: number = 100;

Build:

Subtotal: $75
Shipping: $10
Total: $85

Rules:

  • If subtotal >= free_shipping_threshold, set shipping to 0; otherwise set it to shipping_cost.
  • total = subtotal + shipping.
  • Always include all three lines (Subtotal, Shipping, Total). When shipping is free, the line shows $0: for a subtotal of 120 the lines would be Subtotal: $120, Shipping: $0, Total: $120.
  • Join the three lines with a newline "\n" into one string result, then print it with console.log(result).

Decide shipping in an if/else first, then build the three lines.

Declare let shipping: number; before the if, so it is still visible after the if/else, then assign it in both branches.

In TypeScript "Total: $" + total converts the number to text for you.

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);

STEP 1 OF 11
    Ctrl + Enter to check

    TYPESCRIPT · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V3 in TypeScript, explained

    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

    1. Declare shipping with let and no value. It gets its value in one of two branches, so it cannot be a const with an initializer. Declaring it before the if also keeps it visible afterwards.
    2. Write the if with its condition and close its block right away, so the braces always match.
    3. In the if branch, set shipping to 0. The threshold is met, so the customer pays nothing.
    4. Add the else branch, on the same line as the closing brace of the if.
    5. In the else branch, set shipping to shipping_cost.
    6. Compute total as subtotal + shipping. When shipping is free this equals the subtotal.
    7. Build line1, line2 and line3, each a label followed by a number. The + operator converts the number to text when the other operand is a string.
    8. Join the lines into result with "\n" between them: two newlines for three lines, and none at the end.
    9. Print result once.

    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 holds undefined. If the else branch were missing and the cart were under the threshold, subtotal + shipping would be 75 + undefined, and the program would print Total: $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, not 75.0.

    Edge cases and common mistakes

    • Using > instead of >=. With a subtotal of exactly 100 the cart must ship free. The solution prints Shipping: $0 and Total: $100 for 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_cost to the total instead of shipping. Then a free cart would still be charged.
    • Declaring shipping inside a branch. A let or const declared inside the braces of the if exists 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 with toFixed(2), before printing.

    Testing

    Try changing subtotal in the editor above. These results come from the reference solution:

    subtotalShipping lineTotal line
    75Shipping: $10Total: $85
    99Shipping: $10Total: $109
    100Shipping: $0Total: $100
    120Shipping: $0Total: $120
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free