This is a pretty basic SIP
calculator that should not take more than 5 lines. But the variable amount is constantly being treated as a string
. I have to keep using parseFloat()
and declare three additional variables to store the final values before returning them for the code to work. Is there any workaround?
function sipCalculator(amount, r, n) {
r = r / 12;
amount = parseFloat(amount);
var temp = 0;
for (var i = 0; i < n; i++) {
temp += amount;
temp += (temp * (r / 100));
}
var x = amount * n;
var y = parseFloat(temp.toFixed(2));
var z = parseFloat((y - x).toFixed(2));
return [x, y, z];
};