patternjavascriptMinor
Australian income tax calculator
Viewed 0 times
incomecalculatortaxaustralian
Problem
I'm looking for some feedback on my first Javascript app. I've built an app that calculates tax for Australian tax payers. Specific feedback on areas that could be object orientated and any errors I have made would be great.
```
//Inputs
var income = 250000;
var superAnnuationPercentage = 9.25;
var superAnnuationTaxRate = 15;
//Defines the tax brackets
var taxBracket = [
{from: 0, to: 18200, percentage: 0, amount: 0},
{from: 18201, to: 37000, percentage: 19, over: 18200, amount: 0},
{from: 37001, to: 80000, percentage: 32.5, over: 37000, amount: 3752},
{from: 80001, to: 180000, percentage: 37, over: 80000, amount: 17547},
{from: 180001, to: Infinity, percentage: 45, over: 180000, amount: 54547}
];
//Defines HECS/HELP repayment brackets
var hecsHelp = [
{from:0, to:53345, percentage:4},
{from:53344, to:59421, percentage:4},
{from:59422, to:65497, percentage:4.5},
{from:65498, to:68939, percentage:5.0},
{from:68940, to:74105, percentage:5.5},
{from:74106, to:80527, percentage:6.0},
{from:80258, to:84481, percentage:6.5},
{from:84482, to:92970, percentage:7.0},
{from:99070, to:99069, percentage:7.5},
{from:99070, to:Infinity, percentage:8.0},
];
//Calculates superannuation based on a percentage
var superannuationCalc = function (int) {
var percent = superAnnuationPercentage/100;
return int * percent;
};
//define income after removing superannuation element. Superannuation is taxed at a lower rate.
var grossIncome = income - superannuationCalc(income);
//The end result of alll the code.
var workoutTax = taxCalc(grossIncome);
//bringing it all together
function workOutSuperTax(){
var a = superannuationCalc(income);
var b = superAnnuationTaxRate;
var c = superannuationTaxCalc(a,b);
return c;
}
var superTax = workOutSuperTax();
var workOutHECS = hecsHelpCalc(income);
totalTax = workoutTax + superTax + workOutHECS;
//The end result!
console.log(totalTax);
//Loops through taxBracket object and finds the tax bracket for the post de
```
//Inputs
var income = 250000;
var superAnnuationPercentage = 9.25;
var superAnnuationTaxRate = 15;
//Defines the tax brackets
var taxBracket = [
{from: 0, to: 18200, percentage: 0, amount: 0},
{from: 18201, to: 37000, percentage: 19, over: 18200, amount: 0},
{from: 37001, to: 80000, percentage: 32.5, over: 37000, amount: 3752},
{from: 80001, to: 180000, percentage: 37, over: 80000, amount: 17547},
{from: 180001, to: Infinity, percentage: 45, over: 180000, amount: 54547}
];
//Defines HECS/HELP repayment brackets
var hecsHelp = [
{from:0, to:53345, percentage:4},
{from:53344, to:59421, percentage:4},
{from:59422, to:65497, percentage:4.5},
{from:65498, to:68939, percentage:5.0},
{from:68940, to:74105, percentage:5.5},
{from:74106, to:80527, percentage:6.0},
{from:80258, to:84481, percentage:6.5},
{from:84482, to:92970, percentage:7.0},
{from:99070, to:99069, percentage:7.5},
{from:99070, to:Infinity, percentage:8.0},
];
//Calculates superannuation based on a percentage
var superannuationCalc = function (int) {
var percent = superAnnuationPercentage/100;
return int * percent;
};
//define income after removing superannuation element. Superannuation is taxed at a lower rate.
var grossIncome = income - superannuationCalc(income);
//The end result of alll the code.
var workoutTax = taxCalc(grossIncome);
//bringing it all together
function workOutSuperTax(){
var a = superannuationCalc(income);
var b = superAnnuationTaxRate;
var c = superannuationTaxCalc(a,b);
return c;
}
var superTax = workOutSuperTax();
var workOutHECS = hecsHelpCalc(income);
totalTax = workoutTax + superTax + workOutHECS;
//The end result!
console.log(totalTax);
//Loops through taxBracket object and finds the tax bracket for the post de
Solution
Ranges
You use
to define ranges with different percentages. A few things to say about this :
I think things would be easier if you were to define it like :
Then, iterating backward (or forward if you define your array as the reverse of what I have written), everything should be easier to maintain.
You use
var hecsHelp = [
{from:0, to:53345, percentage:4},
{from:53344, to:59421, percentage:4},
{from:59422, to:65497, percentage:4.5},
{from:65498, to:68939, percentage:5.0},
{from:68940, to:74105, percentage:5.5},
{from:74106, to:80527, percentage:6.0},
{from:80258, to:84481, percentage:6.5},
{from:84482, to:92970, percentage:7.0},
{from:99070, to:99069, percentage:7.5},
{from:99070, to:Infinity, percentage:8.0},
];to define ranges with different percentages. A few things to say about this :
- it does not respect the DRY principle : this makes things easier to get wrong (and I believe that either the first or the second range is indeed wrong as it does not follow the same pattern where
range[n].to + 1 == range[n+1].from)
- it does not define what happens when the number you want to handle happens to be between
range[n].toandrange[n+1].fromwhich can happen if you are handling non-integers values.
I think things would be easier if you were to define it like :
var hecsHelp = [
{from:0, percentage:4},
{from:53344, percentage:4}, # By the way, is this line useful ?
{from:59422, percentage:4.5},
{from:65498, percentage:5.0},
{from:68940, percentage:5.5},
{from:74106, percentage:6.0},
{from:80258, percentage:6.5},
{from:84482, percentage:7.0},
{from:99070, percentage:7.5}, # This one seems pretty weird too.
{from:99070, percentage:8.0},
];Then, iterating backward (or forward if you define your array as the reverse of what I have written), everything should be easier to maintain.
Code Snippets
var hecsHelp = [
{from:0, to:53345, percentage:4},
{from:53344, to:59421, percentage:4},
{from:59422, to:65497, percentage:4.5},
{from:65498, to:68939, percentage:5.0},
{from:68940, to:74105, percentage:5.5},
{from:74106, to:80527, percentage:6.0},
{from:80258, to:84481, percentage:6.5},
{from:84482, to:92970, percentage:7.0},
{from:99070, to:99069, percentage:7.5},
{from:99070, to:Infinity, percentage:8.0},
];var hecsHelp = [
{from:0, percentage:4},
{from:53344, percentage:4}, # By the way, is this line useful ?
{from:59422, percentage:4.5},
{from:65498, percentage:5.0},
{from:68940, percentage:5.5},
{from:74106, percentage:6.0},
{from:80258, percentage:6.5},
{from:84482, percentage:7.0},
{from:99070, percentage:7.5}, # This one seems pretty weird too.
{from:99070, percentage:8.0},
];Context
StackExchange Code Review Q#61770, answer score: 6
Revisions (0)
No revisions yet.