Calculate the years end bonus with Javascript

Our Own Score

In this tutorial we will write a simple program which will get the input of both the salary of a person and the bonus he will receive and then calculate his years end bonus amount. This program involves the ECMAScript 6 and standard Javascript.

The program will get used of the __proto__ property of an object along with the getter and the setter methods to get the outcome. The entire script is as follows:

let yearendbonus = {__proto__ : {salary : 0, bonus : 0}, get income() {return this.salary * this.bonus;}, set bonusrate(bonus) {this.bonus = bonus;}, set salary_(salary) {this.salary = salary;} };

yearendbonus.bonusrate = 1.6;
yearendbonus.salary_ = 5000;

console.log("Basic plus bonus is " + yearendbonus.income); // will calculate the output which is 8000

The outcome is as follows:

Basic plus bonus is 8000

The program above will only run on the ECMAScript 6 which has now been supported in all the major web browser nowadays.


Leave a Reply

Notify of