In this article, we will learn how to create a JavaScript program that will ask the user to enter their basic salary and then calculate the bonus amount which will be 20% of their basic salary or base pay. The gross salary is the bonus amount + basic salary.
bonus amount = 20% of basic salary gross salary = bonus amount + basic salary
Approach: In this code, we will be displaying an input box to the user which will prompt the user to enter the basic pay. On clicking the submit button, the calculate function is called which computes the bonus amount and the gross amount and displays it to the user using the DOM elements in JavaScript.
Examples:
Input 1: 50000 Output: Bonus: 10000 Gross: 60000 Input 2: 2500 Output: Bonus: 500 Gross: 3000 Input 3: 32500 Output: Bonus: 6500 Gross: 39000
Below is the implementation of the above approach.
Javascript
<script> ( function calculate() { var bpay, bonus, gross; bpay = 50000; //bonus is 20% of basic salary bonus = 0.2 * bpay; gross = bonus + bpay; console.log( "Basic Pay: " + bpay); console.log( "Bonus: " + bonus); console.log( "Gross: " + gross); })(); </script> |
Output:
Basic Pay: 50000 Bonus: 10000 Gross: 60000