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
- Compute the bonus. Create a
constnamedbonusfromsalary * 15 / 100and wrap it inMath.floor. Useconstbecause the value never changes after this line. - Print the name.
console.log(name)prints the string without quotes, on its own line. - Print the bonus line. Join the text
"Bonus: $"andbonuswith+. 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.floorrounds 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, whileMath.trunc(-1.5)is-1.salary * 15 / 100is 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 / 100is a fraction, not zero. Dividing two integers doesn't truncate, which is why the explicitMath.flooris needed. - Declaring
productivitywithout using it is not an error in JavaScript.
Edge cases and common mistakes
- Forgetting to round. Without
Math.floor, a salary such as 10010 printsBonus: $1501.5instead ofBonus: $1501. - Printing both values in one call with a comma.
console.log("Bonus: $", bonus)puts a space between the arguments and printsBonus: $ 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
letorvarfor everything. It works, butconsttells the reader the value is fixed, and assigning to it again raisesAssignment 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.
