Skip to content

Instantly share code, notes, and snippets.

@vcolesdev
Last active February 5, 2023 00:34
Show Gist options
  • Save vcolesdev/caf24dba586a0fe944db94e094b54fe1 to your computer and use it in GitHub Desktop.
Save vcolesdev/caf24dba586a0fe944db94e094b54fe1 to your computer and use it in GitHub Desktop.
Salary calculator based on taxes for CA residents written in Vanilla JavaScript. TODO: Account for federal tax rates.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>VC Salary Calculator</title>
</head>
<body>
<h1>VC Salary Calculator</h1>
<script src="SalaryCalculator.js"></script>
<script>
// Example
const yearlySalary = new SalaryCalculator({
rate: 15.50,
overtime: 5,
vacationDays: 10
})
console.log('Gross Salary: ' + yearlySalary.gross)
console.log('Gross Salary (Short): ' + yearlySalary.grossShortened)
console.log('Take Home Pay: ' + yearlySalary.afterTaxes)
console.log('Take Home Pay (Short): ' + yearlySalary.afterTaxesShortened)
</script>
</body>
</html>
// Constants
const WORK_HOURS = 8
const TOTAL_WORK_HOURS = 2088
//const TOTAL_WORK_DAYS = 261
//const TOTAL_WORK_WEEKS = 48
const CA_STATE_TAXES = 25.6 / 100
const CA_STATE_INS_TAXES = 8.56 / 100
/**
* SalaryCalculator()
*
* Calculate total yearly salary by taking the hourly rate,
* overtime/holiday hours worked, and subtracting the sum
* by vacation days taken. CA Taxes are deducted from this
* total to represent "take home" pay before federal taxes.
*
* @author: Vanessa Coles (VMC Design)
* @license: MIT
*
* @constructor Unpaid vacation time
*
* @return {Object} Various methods that return gross salary and salary after taxes in different formats.
*/
function SalaryCalculator ({ rate, overtime, vacationDays }) {
this.rate = rate
this.overtime = overtime
this.vacationDays = vacationDays
/**
* Round two decimal places.
* @param float
* @returns {number}
*/
roundTwoPlaces = float => Math.round(float * 100) / 100
/**
* Format numbers into Cash strings.
* @param num
* @returns {*|string}
*/
formatCash = num => {
if (num < 1e3) return num // 1e3 = 10^3 = 1000
if (num >= 1e3) return +(num /1e3).toFixed(1) + 'K'
}
return {
gross: '$' + this.calcTotalSalary().toLocaleString('en-US'),
grossShortened: formatCash(this.calcTotalSalary()),
afterTaxes: '$' + this.calcSalaryAfterCaTaxes().toLocaleString('en-US'),
afterTaxesShortened: formatCash(this.calcSalaryAfterCaTaxes())
}
}
/**
* calculateSalary()
*
* Calculate our yearly salary.
*/
SalaryCalculator.prototype.calcSalary = function() {
return roundTwoPlaces(this.rate * TOTAL_WORK_HOURS)
}
/**
* calculateOvertimePay()
*
* Calculate the total amount paid for overtime.
*/
SalaryCalculator.prototype.calcOvertimePay = function() {
return roundTwoPlaces((this.rate * this.overtime) * 1.5)
}
/**
* calculateLostVacationPay()
*
* Calculate the amount of pay lost for vacation days.
*/
SalaryCalculator.prototype.calcLostVacationPay = function() {
const vacationHours = this.vacationDays * WORK_HOURS
const lostVacationPay = this.rate * vacationHours
return roundTwoPlaces(lostVacationPay)
}
/**
* calculateTotalSalary()
*
* Calculate the total salary minus unpaid vacation time.
*/
SalaryCalculator.prototype.calcTotalSalary = function() {
const salary = this.calcSalary()
const overtime = this.calcOvertimePay()
const lostVacationPay = this.calcLostVacationPay()
return (salary + overtime) - lostVacationPay
}
/**
* calcSalaryAfterTaxes()
*
* Calculate total salary after CA taxes.
*/
SalaryCalculator.prototype.calcSalaryAfterCaTaxes = function() {
const caTaxPercent = CA_STATE_TAXES + CA_STATE_INS_TAXES
const calcTaxesRemoved = this.calcTotalSalary() * caTaxPercent
return roundTwoPlaces(this.calcTotalSalary() - calcTaxesRemoved)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment