Start learning free

LESSON 58 OF 289 · CALCULATIONS - CHALLENGES

Employee of the Year V1 in TypeScript: One Employee

Practice Read the full explanation

Compute an end-of-year performance bonus for one employee, then print the employee's name and the bonus.

You start with:

const name: string = "John Doe";
const productivity: number = 83;
const salary: number = 135000;

Build:

John Doe
Bonus: $20250

Rules:

  • The bonus is 15% of the salary, rounded down to a whole number.
  • Print the employee's name on the first line.
  • Print the bonus on the second line as "Bonus: $<amount>".
  • productivity is not used in this version. It is there for the next versions, where you will compare employees by productivity.

Use Math.floor to round the 15% down to a whole number: const bonus: number = Math.floor(salary * 15 / 100);

+ turns the number into text: console.log("Bonus: $" + bonus);

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

STEP 1 OF 3
    Ctrl + Enter to check

    TYPESCRIPT · CALCULATIONS - CHALLENGES

    Employee Of The Year V1 in TypeScript, explained

    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

    1. Create bonus. Declare a const typed as number, multiply salary by 15, divide by 100 and wrap the whole calculation in Math.floor. It is a const because the value is computed once and never reassigned.
    2. Print the name. A single console.log(name) writes the first output line. Each call to console.log ends its line, so two calls give two lines.
    3. 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. : string and : number are checked by tsc and 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: $" + bonus is concatenation, and the output is Bonus: $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.floor a salary like 135001 has an exact bonus of 20250.15. With 135001, whose exact bonus is 20250.15, the unrounded version prints Bonus: $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.log calls. Joining them into one string without a "\n" would put them on one line.
    • Using the wrong variable. The bonus comes from salary, not productivity, which this version leaves unused.

    Testing

    • salary = 135000 prints John Doe and Bonus: $20250.
    • salary = 100000 prints John Doe and Bonus: $15000.
    • salary = 99 prints John Doe and Bonus: $14.
    • salary = 135001 prints John Doe and Bonus: $20250.
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free