Number of calculation iterations based on user input
-
Please see here:
https://onemoredime.com/net-worth-calculator/
First Question: This is what I am trying to do: someone can enter their current age and target age, current net worth, savings per year, and calculated the net worth at target date.
For example – someone who is 40 years old, has $100, saves 0 per year, target age = 41, net worth at target age will be 108 (100*1.08). This is working in the form today.
The problem is about the savings per year – if the two ages (Current and target) were 40 and 45, current net worth = 0, savings per year = 25, then how do I calculate the final net worth?
I know it will be the following:
25*pow(1.08,5)+
25*pow(1.08,4)+
25*pow(1.08,3)+
25*pow(1.08,2)+
25*pow(1.08,1)First year savings compounded over 5 years (age 40 – 45), second year savings compounded for 4 years (age 41 – 45), so on and so forth.
But the problem is user will enter the 2 ages. If I hard code the above for 5 years, what if the next user to my website enters ages 40 and 46…. then there need to be 6 terms (in stead of the 5 in the above equation).
I can start with
‘fieldname target age’ – ‘fieldname current age’ and ‘fieldname target age’ – ‘fieldname current age -1’
….etcBut how will the form know where to STOP and not go into negative numbers.
Thank you in advance
- The topic ‘Number of calculation iterations based on user input’ is closed to new replies.