Start learning free

LESSON 186 OF 671 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V1 in JavaScript: A One-Item Receipt

Practice Read the full explanation

Practice variables, arithmetic, and string concatenation by printing a tiny one-item receipt.

You start with:

const item = "Coffee Mug";
const price = 12;
const quantity = 3;

Build:

Coffee Mug x3
Total: $36

Rules:

  • Compute total = price * quantity.
  • First line: "<item> x<quantity>" (e.g. "Coffee Mug x3").
  • Second line: "Total: $<total>".
  • Join the two lines with a newline "\n" into one string result, then print it with console.log(result).

JavaScript's + converts a number to text when the other side is a string, so item + " x" + quantity just works.

line1 + "\n" + line2 joins both lines into one string.

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

STEP 1 OF 5
    Ctrl + Enter to check

    JAVASCRIPT · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V1 in JavaScript, explained

    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

    1. Compute total as price * quantity. It is a const, because it is calculated once.
    2. 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 the x.
    3. Build line2, the total row: "Total: $" followed by total.
    4. Build result. Join line1, the newline "\n" and line2.
    5. Print result with console.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, so item + " x" + quantity gives "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: total comes before line2, which uses it, and line1 and line2 come before result.

    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 + quantity gives 15, not 36. The total is a product.
    • Forgetting the space in " x", which prints Coffee Mugx3.
    • Quantity 0. The total is 0, and the receipt shows Coffee Mug x0 and Total: $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 raises Assignment to constant variable.

    Testing

    • "Pen", price 2, quantity 5 gives Pen x5 and Total: $10.
    • "Lamp", price 40, quantity 1 gives Lamp x1 and Total: $40.
    Keep going with JavaScript650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free