Problem overview
This is the first version of a small program. Here it handles a single employee. The data sits in three constants: name, productivity and salary. The program computes the employee's end-of-year bonus, which is 15% of the salary rounded down to a whole number, and prints two lines: the name, then Bonus: $ followed by the amount.
For John Doe with a salary of 135000, 15% is 20250, so the output is:
John Doe
Bonus: $20250
The productivity constant isn't used yet. It is there because the next versions compare employees by productivity.
Approach
The program has three small jobs: calculate the bonus, print the name, print the bonus line. Calculating first and printing afterwards keeps the arithmetic in one place.
The percentage is written as salary * 15 / 100: multiply by the rate first, then divide by 100. In TypeScript every number is a double-precision floating-point value, so / never truncates. A salary of 135001 gives 20250.15, not 20250. To get a whole number you round explicitly, and Math.floor rounds down, which is what the rules ask for.
Step by step
- Create
bonus. Declare aconsttyped asnumber, multiplysalaryby 15, divide by 100 and wrap the whole calculation inMath.floor. It is aconstbecause the value is computed once and never reassigned. - Print the name. A single
console.log(name)writes the first output line. Each call toconsole.logends its line, so two calls give two lines. - Print the bonus line. Join the text
"Bonus: $"and the number with+. When one side of+is a string, JavaScript converts the other side to text, so no explicit conversion is needed. The dollar sign is an ordinary character inside the quotes.
The solution in TypeScript
const name: string = "John Doe";
const productivity: number = 83;
const salary: number = 135000;
const bonus: number = Math.floor(salary * 15 / 100);
console.log(name);
console.log("Bonus: $" + bonus);
A few details are worth knowing:
- Annotations are for the compiler only.
: stringand: numberare checked bytscand then erased. The JavaScript that runs has no types, so they convert nothing. +does two jobs. Between two numbers it adds; as soon as a string is involved it concatenates."Bonus: $" + bonusis concatenation, and the output isBonus: $20250.- Template literals work too.
`Bonus: $${bonus}`prints the same text, but this lesson uses+, which is the shorter path to the same result here.
Edge cases and common mistakes
- Forgetting to round. Without
Math.floora salary like 135001 has an exact bonus of 20250.15. With 135001, whose exact bonus is 20250.15, the unrounded version printsBonus: $20250.15, while the lesson's salary divides evenly and hides the mistake. - Printing both on one line. The name and the bonus line are separate
console.logcalls. Joining them into one string without a"\n"would put them on one line. - Using the wrong variable. The bonus comes from
salary, notproductivity, which this version leaves unused.
Testing
salary = 135000printsJohn DoeandBonus: $20250.salary = 100000printsJohn DoeandBonus: $15000.salary = 99printsJohn DoeandBonus: $14.salary = 135001printsJohn DoeandBonus: $20250.
