When this code runs, it outputs "Annual Account Balance for Year (i) = 100" for every year. Clearly the answer shouldn't be 100 every time, but I can't see what I am doing wrong in the equation.
var invest;
var rate;
var amount;
rate = 0.08;
invest = 100,000;
for(var i=1;i<=10;i++){
amount = ((parseInt(invest)) * (1.0 + (parseInt(rate)) * (parseInt(i))))
document.write("Annual Account Balance for Year " + i + " = " + amount + "<br>");
}
Instead of calling
parseInt(rate)
, you need to be callingparseFloat(rate)
- though according to your code, these are numbers - so they don't need to be parsed regardless.parseInt
will parse an Integer from a string (though it will still be of type 'Number'). In this case, it is actually returning 0 instead of the 0.08 that you want to be using. CallingparseInt
on a number will convert it from a number to a string, and then back again... Which is essentially useless.Additionally, as Oriol stated, you can't add commas to numbers in JavaScript.