Created
January 1, 2024 11:09
-
-
Save programaths/d8361046257384c952e81577d47a65b7 to your computer and use it in GitHub Desktop.
Tax calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Tax Calculator</title> | |
<script type="module" src="tax-calculator.js"></script> | |
<script type="module" src="main.js"></script> | |
</head> | |
<body> | |
<div> | |
<label for="income">Revenus (gross)</label><input type="number" min="0" id="income"> | |
</div> | |
<div> | |
<div>Montant de la taxe:<span id="tax-amount"></span></div> | |
<div>Taxe effective:<span id="tax-effective"></span></div> | |
</div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {calculateTax} from "./tax-calculator.js"; | |
const income=document.querySelector('#income'); | |
const taxAmount=document.querySelector('#tax-amount'); | |
const taxEffective=document.querySelector('#tax-effective'); | |
income.addEventListener('change',()=>{ | |
let tax=calculateTax(+income.value||0,[ | |
{upperBound:11294,taxPercent:0}, | |
{upperBound:28797,taxPercent:11}, | |
{upperBound:78570,taxPercent:30}, | |
{upperBound:177106,taxPercent:41}, | |
{upperBound:Infinity,taxPercent:45}, | |
],0); | |
taxAmount.innerText=tax+'€'; | |
if(income.value===0){ | |
taxEffective.innerText=0+'%'; | |
}else{ | |
taxEffective.innerText=tax/income.value*100+'%'; | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* @param amountToTax {number} | |
* @param brackets {Array[{upperBound:number,taxPercent:number}]} Upper bound of each bracket. | |
* @param exoneratedAmount {number} | |
* @return {number} | |
*/ | |
export function calculateTax(amountToTax, brackets, exoneratedAmount) { | |
const realTaxableAmount=amountToTax-exoneratedAmount; | |
if(realTaxableAmount<=0){ | |
return 0; | |
} | |
let previousUpperBound=0; | |
let taxAmount=0; | |
for(let {upperBound,taxPercent} of brackets){ | |
if(realTaxableAmount<upperBound){ | |
taxAmount+= (realTaxableAmount-previousUpperBound)*taxPercent/100; | |
break; | |
}else{ | |
taxAmount+= (upperBound-previousUpperBound)*taxPercent/100; | |
} | |
previousUpperBound=upperBound; | |
} | |
return taxAmount; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment