Problem overview
The Shopping Cart project starts with the smallest possible receipt. The starter code defines item, price and quantity. The program computes the total cost and prints two lines: the item with its quantity, and the total.
Coffee Mug x3
Total: $36
Three mugs at 12 each cost 36. Both lines are joined into one string called result with a newline between them, and printed with a single console.log(result).
Approach
Work in two parts: calculate, then format. The calculation is price * quantity. The formatting turns the values into the two rows of the receipt. Each row gets its own variable, line1 and line2, and result joins them. A variable per row keeps each piece small and easy to check.
Step by step
- Compute
totalasprice * quantity. It is aconst, because it is calculated once. - Build
line1, the item row. It is the item name, the text" x"and the quantity, joined with+. The space belongs to the text piece before thex. - Build
line2, the total row:"Total: $"followed bytotal. - Build
result. Joinline1, the newline"\n"andline2. - Print
resultwithconsole.log.
Tracing the starter data
With price = 12 and quantity = 3, total is 36. line1 is "Coffee Mug" joined with " x" and 3, which is "Coffee Mug x3". line2 is "Total: $36". The string in result is "Coffee Mug x3\nTotal: $36", and console.log shows the newline as a line break.
The solution in JavaScript
const item = "Coffee Mug";
const price = 12;
const quantity = 3;
const total = price * quantity;
const line1 = item + " x" + quantity;
const line2 = "Total: $" + total;
const result = line1 + "\n" + line2;
console.log(result);
Details that matter:
- When
+has a string on one side, JavaScript converts the other side to a string, soitem + " x" + quantitygives"Coffee Mug x3". If both sides are numbers,+adds them instead. "\n"is a single newline character. A string that contains it displays as two lines when printed.- The dollar sign in
"Total: $"is an ordinary character. It only has a special meaning inside template literals, when followed by a brace, and this solution doesn't use template literals. - The order of the lines in the code is the order of dependence:
totalcomes beforeline2, which uses it, andline1andline2come beforeresult.
If the quantity or the price changes in the starter lines, nothing else in the program needs editing. Every row is built from the variables, so the receipt always matches the data. That is the benefit of naming each value once and reusing the name.
The rows in the lesson are built as separate variables so each step can be checked on its own: once total exists, line1 and line2 only format values that are already known.
Edge cases and common mistakes
- Using
+to compute the total.price + quantitygives 15, not 36. The total is a product. - Forgetting the space in
" x", which printsCoffee Mugx3. - Quantity 0. The total is 0, and the receipt shows
Coffee Mug x0andTotal: $0. - Printing the two lines with separate arguments, as in
console.log(line1, line2). That prints them on one line, separated by a space. The"\n"is what separates the rows. - Reassigning a
const. It raisesAssignment to constant variable.
Testing
"Pen", price 2, quantity 5 givesPen x5andTotal: $10."Lamp", price 40, quantity 1 givesLamp x1andTotal: $40.
