Start learning free

LESSON 189 OF 289 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V1 in TypeScript: Single Item Total

Practice Read the full explanation

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

You start with:

const item: string = "Coffee Mug";
const price: number = 12;
const quantity: number = 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).

TypeScript'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: string = "Coffee Mug";
const price: number = 12;
const quantity: number = 3;

const total: number = price * quantity;
const line1: string = item + " x" + quantity;
const line2: string = "Total: $" + total;
const result: string = line1 + "\n" + line2;
console.log(result);

STEP 1 OF 5
    Ctrl + Enter to check

    TYPESCRIPT · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V1 in TypeScript, explained

    Problem overview

    This project prints a tiny one-item receipt. The starter code holds three constants: item, price and quantity. The program computes the total, builds two lines of text, joins them into one string called result and prints it with console.log.

    The first line is the item and the quantity, as in Coffee Mug x3. The second is Total: $ followed by the price times the quantity. For a Coffee Mug at 12, bought 3 times, the output is:

    Coffee Mug x3
    Total: $36

    Approach

    The work splits into small named pieces: the number first, then each line of text, then the joined result. Each piece is a const with its own name, so every statement does one thing and can be read on its own.

    Two operators carry the lesson. * multiplies the price by the quantity. + joins text, and when one side is a string and the other a number, JavaScript turns the number into text. That is why item + " x" + quantity works with no explicit conversion.

    It helps to compute the pieces by hand for the starter data. The total is 12 times 3, which is 36. The first line is Coffee Mug, then " x", then 3, so it reads Coffee Mug x3. The second line is Total: $ followed by 36. Joined with a newline, the two lines make up the whole output.

    The order of the statements matters. A const must be declared before the line that uses it, so total comes before line2, and both lines come before result.

    Step by step

    1. Compute total. A const typed as number, set to price * quantity. It is the cost of all the items.
    2. Build line1. A string made of item, the text " x" (with its leading space) and quantity.
    3. Build line2. A string made of "Total: $" and total. The dollar sign is an ordinary character inside the quotes.
    4. Join into result. line1, then "\n", then line2. The \n escape is the newline character, so the text breaks into two rows where it sits.
    5. Print result with console.log. One call prints both rows because the newline is part of the string.

    The solution in TypeScript

    const item: string = "Coffee Mug";
    const price: number = 12;
    const quantity: number = 3;
    
    const total: number = price * quantity;
    const line1: string = item + " x" + quantity;
    const line2: string = "Total: $" + total;
    const result: string = line1 + "\n" + line2;
    console.log(result);
    

    Details to keep in mind:

    • Types are checked, then erased. : number and : string are for the compiler and the editor. In the JavaScript that runs they are gone, and nothing is converted by them.
    • + adds or joins. Between two numbers it adds, and with a string on either side it joins. Because the expressions here start with a string, every + in them joins.
    • const. Each value is computed once and never reassigned, so const says so.
    • Numbers print without extra formatting. The total 36 appears as 36, with no decimals.

    Edge cases and common mistakes

    • A missing space. Without the space in " x" the first line becomes Coffee Mugx3.
    • Adding instead of multiplying. With price + quantity the total prints as $15 instead of $36.
    • The wrong slash. "/n" is two ordinary characters. The output becomes Coffee Mug x3/nTotal: $36, all on one line.
    • Quantity of zero. With quantity = 0 the output is Coffee Mug x0 and Total: $0.
    • Doing the multiplication inside the string. "Total: $" + price * quantity prints the same Total: $36, because * is evaluated before +. A separate total variable is clearer, and it can be reused.

    Testing

    • Coffee Mug, price 12, quantity 3 prints Coffee Mug x3 and Total: $36.
    • Coffee Mug, price 5, quantity 1 prints Coffee Mug x1 and Total: $5.
    • Pen, price 2, quantity 10 prints Pen x10 and Total: $20.
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free