# Thread: Javascript that calculates % of another calculation

1. Junior Coders Join Date
Apr 2014
Posts
19
Thanks
3
Thanked 0 Times in 0 Posts

## Javascript that calculates % of another calculation

I have code which calculates 8% of the number entered, so for example is in the code below. If you enter "1" in the first box" and "100" in the second box the Total Refund (including 8%) = 108

JavaScript code:
Code:
```\$(document).ready(function() {
var sum = 0;
function calcSum(prevVal) {
var val1 = \$('#val1').val();
var val2 = \$('#val2').val();
var val3 = \$('#val3').val();
var val4 = \$('#val4').val();
this.sum = parseFloat(val1) * parseFloat(val2) + parseFloat(val3) * parseFloat(val4);
return this.sum;
}
var subAmt = \$("#sub"),
taxAmt = \$("#tax"),
totAmt = \$("#total");
\$(".val").each(function() {
var prevVal = this.value / 1,
self = this;
\$(this).keyup(function() {
subAmt.val(calcSum.call(self, prevVal));
totAmt.val(this.sum + this.sum * parseFloat(taxAmt.val() / 100));
prevVal = self.value;
});
});
});```

http://jsfiddle.net/2k1zr59u/3/

What I would like to do is to calculate 20% of the 8% in the second to the bottom box. So in this example the answer should be 1.60.

Then I would like to show in the total box 106.40 (108 - 1.60).
Last edited by Beverleyh; 06-11-2015 at 04:25 PM. Reason: Formatting  Reply With Quote

2. Junior Coders Join Date
Apr 2014
Posts
19
Thanks
3
Thanked 0 Times in 0 Posts

##   Reply With Quote

3. Junior Coders Join Date
Apr 2014
Posts
19
Thanks
3
Thanked 0 Times in 0 Posts

## I received some help and have it working now, thanks  Reply With Quote

javascript 