Start learning free

LESSON 56 OF 671 · CALCULATIONS - CHALLENGES

Employee of the Year V1 in JavaScript: One Bonus

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 = "John Doe";
const productivity = 83;
const salary = 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 = Math.floor(salary * 15 / 100);

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

const name = "John Doe";
const productivity = 83;
const salary = 135000;

const bonus = Math.floor(salary * 15 / 100);
console.log(name);
console.log("Bonus: $" + bonus);

STEP 1 OF 3
    Ctrl + Enter to check

    JAVASCRIPT · CALCULATIONS - CHALLENGES

    Employee Of The Year V1 in JavaScript, explained

    Problem overview

    This first version of the Employee of the Year project handles a single employee. The starter code gives three constants: name, productivity and salary. The program computes a bonus of 15% of the salary, rounded down to a whole number, and prints two lines: the name, then Bonus: $ followed by the amount.

    With a salary of 135000, 15% is 20250, so the output is:

    John Doe
    Bonus: $20250
    

    The productivity constant stays unused for now. Later versions compare employees by it, so it is already in the data.

    Approach

    A percentage of a number is the number times the percentage divided by 100. Multiplying by 15 first and dividing by 100 afterwards gives the exact quotient, which may have decimals. Math.floor removes them by rounding down to the nearest whole number. For 135000 the result is already whole, but for other salaries it often isn't: a salary of 10010 gives 1501.5, and Math.floor turns it into 1501.

    The rest is output. Two console.log calls produce two lines, because each call ends its output with a newline.

    Step by step

    1. Compute the bonus. Create a const named bonus from salary * 15 / 100 and wrap it in Math.floor. Use const because the value never changes after this line.
    2. Print the name. console.log(name) prints the string without quotes, on its own line.
    3. Print the bonus line. Join the text "Bonus: $" and bonus with +. When one side of + is a string, JavaScript converts the other side to text, so the number becomes part of the string. The dollar sign is an ordinary character inside normal quotes.

    The solution in JavaScript

    const name = "John Doe";
    const productivity = 83;
    const salary = 135000;
    
    const bonus = Math.floor(salary * 15 / 100);
    console.log(name);
    console.log("Bonus: $" + bonus);
    

    A few details about this code:

    • Math.floor rounds toward negative infinity. For the positive amounts here that is the same as cutting off the decimals. For a negative number, Math.floor(-1.5) is -2, while Math.trunc(-1.5) is -1.
    • salary * 15 / 100 is evaluated from left to right, so the multiplication happens before the division. Writing the whole product first keeps the numbers whole until the final division.
    • Numbers in JavaScript are all doubles, so 15 / 100 is a fraction, not zero. Dividing two integers doesn't truncate, which is why the explicit Math.floor is needed.
    • Declaring productivity without using it is not an error in JavaScript.

    Edge cases and common mistakes

    • Forgetting to round. Without Math.floor, a salary such as 10010 prints Bonus: $1501.5 instead of Bonus: $1501.
    • Printing both values in one call with a comma. console.log("Bonus: $", bonus) puts a space between the arguments and prints Bonus: $ 20250. Joining with + gives no extra space.
    • Leaving out the space after the colon. The text must be "Bonus: $", with one space between the colon and the dollar sign.
    • Using let or var for everything. It works, but const tells the reader the value is fixed, and assigning to it again raises Assignment to constant variable.

    Testing

    • A salary of 135000 gives Bonus: $20250.
    • A salary of 10010 gives Bonus: $1501.
    • A salary of 0 gives Bonus: $0.
    Keep going with JavaScript650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free