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
- Compute
total. Aconsttyped asnumber, set toprice * quantity. It is the cost of all the items. - Build
line1. Astringmade ofitem, the text" x"(with its leading space) andquantity. - Build
line2. Astringmade of"Total: $"andtotal. The dollar sign is an ordinary character inside the quotes. - Join into
result.line1, then"\n", thenline2. The\nescape is the newline character, so the text breaks into two rows where it sits. - Print
resultwithconsole.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.
: numberand: stringare 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, soconstsays 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 becomesCoffee Mugx3. - Adding instead of multiplying. With
price + quantitythe total prints as$15instead of$36. - The wrong slash.
"/n"is two ordinary characters. The output becomesCoffee Mug x3/nTotal: $36, all on one line. - Quantity of zero. With
quantity = 0the output isCoffee Mug x0andTotal: $0. - Doing the multiplication inside the string.
"Total: $" + price * quantityprints the sameTotal: $36, because*is evaluated before+. A separatetotalvariable is clearer, and it can be reused.
Testing
- Coffee Mug, price 12, quantity 3 prints
Coffee Mug x3andTotal: $36. - Coffee Mug, price 5, quantity 1 prints
Coffee Mug x1andTotal: $5. - Pen, price 2, quantity 10 prints
Pen x10andTotal: $20.
