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 stringresult, then print it withconsole.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);
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
