Problem overview
The program starts with three numbers: a subtotal of 75, a flat shipping_cost of 10 and a free_shipping_threshold of 100. It prints a three-line receipt: the subtotal, the shipping and the total. Carts at or above the threshold ship for free, so their shipping line shows $0. The other lines stay in the receipt either way.
With the starting values, the cart is below the threshold, so it pays shipping:
Subtotal: $75
Shipping: $10
Total: $85
The earlier versions of this project printed fixed receipts. This one makes its first decision: the value of shipping depends on the data.
Approach: decide the shipping first, then build the text
Everything after the shipping depends on it: the total adds it, and the middle line prints it. So the plan is to settle shipping with an if/else and only then compute total and the three lines. Writing the decision first means the rest of the program reads the same for every cart.
The condition is subtotal >= free_shipping_threshold. It uses >=, not >, because a cart exactly on the threshold counts as free. With a subtotal of 100, the receipt shows Shipping: $0 and a total of 100. At 99 it shows Shipping: $10 and a total of 109.
The threshold and the cost are variables, not numbers typed into the condition. Changing the shop's policy then means editing one line at the top, and the condition keeps reading as what it means: the subtotal reaches the threshold.
Step by step
- Declare
shippingwithletand no value. It gets its value in one of two branches, so it can't be aconst, which must be assigned where it is declared. - Write the
if. Its condition comparessubtotalwith the threshold using>=. Close the block straight away so the braces always match. - Free case. Inside the
if, assign0toshipping. - Open the
else. It runs whenever the condition is false. The brace that closes theifblock is also whereelsegoes, on the same line. - Paid case. Inside the
else, assignshipping_costtoshipping. - Compute
total. It is the subtotal plus the shipping. When shipping is free it adds 0, so the total equals the subtotal. - Build the three lines. Each is a label followed by the number, joined with
+. - Join them into
result. Two"\n"separators go between the three lines. - Print
resultwith oneconsole.log.
The solution in JavaScript
const subtotal = 75;
const shipping_cost = 10;
const free_shipping_threshold = 100;
let shipping;
if (subtotal >= free_shipping_threshold) {
shipping = 0;
} else {
shipping = shipping_cost;
}
const total = subtotal + shipping;
const line1 = "Subtotal: $" + subtotal;
const line2 = "Shipping: $" + shipping;
const line3 = "Total: $" + total;
const result = line1 + "\n" + line2 + "\n" + line3;
console.log(result);
Details worth knowing
let shipping; declares a variable that holds undefined until it is assigned. Because it is declared outside the if, it is still visible after the block. A variable declared with let or const inside the braces would disappear at the closing brace, so declaring shipping there would make the later total line fail.
When + has a string on one side, JavaScript converts the other side to text and joins them. That is why "Total: $" + total works with a number. Be careful with the order, though: "5" + 3 gives "53", while "5" - 3 gives 2, because - always works on numbers. The $ in the string is an ordinary character, because the string uses plain quotes and not backticks.
The three lines are separated by "\n", which is the newline character. There is no newline after the last line, because separators go between lines, not after them.
A shorter form
Once you are comfortable with if/else, the same decision can be written as one expression with the ternary operator: const shipping = subtotal >= free_shipping_threshold ? 0 : shipping_cost;. It gives the same shipping, 10 for a subtotal of 75 and 0 for 100, and it lets shipping be a const. The lesson uses the longer form on purpose: with a statement in each branch, you can see which code runs in which case, and a branch can later hold more than one line.
The two branches here assign the same variable. That is the usual shape of a two-way decision: whichever branch runs, shipping ends up with a value, so the lines below never have to ask which case happened.
Edge cases and common mistakes
- Using
>instead of>=. A cart of exactly 100 would pay 10 for shipping. - Dropping the shipping line when it is free. The receipt always has all three lines, and the free case prints
Shipping: $0. - Declaring
shippinginside a branch. It would not exist after the closing brace. - Using
=in the condition.if (subtotal = free_shipping_threshold)assigns, and since the program declaressubtotalwithconst, running it throws an error. Comparisons use>=,===and the like. - Adding the threshold to the total instead of the shipping, or forgetting that
totalmust be computed after theif/else, whenshippingalready has its value.
Testing
Change subtotal in the editor above and check the receipt. These are the results for three subtotals:
| 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 |
