0

JSFiddle

My script is to calculate a pension via putting in a fixed amount from the day you start employment until the last day based upon pension age, so my DOB is 25/12/1991 therefore if I retire at pension age of 68, it'll be on 25/12/2060.

function hello(){
  var i; var t = 50; var f = 0;
  for (i = 0; i < CalcY; i++) {
    f = ((t*52) + f) * 1.08;
    $('#collection').append("<li>Year: " + i + " Amount: £" + (f).toFixed(2).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") + "</li>");
  }
  TotalPension = ((t*CalcW) + f);
  $('#collection').append("<li>END Amount: £" + (TotalPension).toFixed(2).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") + "</li><br /><br /><br />");
  if (i < CalcY) window.setTimeout(hello, 0);
  hello2();
}

The first part runs through listing how much it'll be at the end of each year with an 8% interest rate gain and then hello2(); is to calculate taking out money each year with interest gained on the remaining.

So hello2(); I need to stop once the investment drops below money taken out each year which seems to be working partly but doesn't stop, it just keeps repeating as if it was calling itself.

Tyler
  • 854
  • 1
  • 10
  • 26
  • This seems to be a maths problem, not sure what it has to do with jQuery? – Bergi Jul 09 '16 at 13:06
  • Still it's not a question about jQuery :-) Have you had a look at a [loop tutorial](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration)? – Bergi Jul 09 '16 at 13:25
  • Please [edit] your question to include your code here instead of linking to it. Hint: a `for` head consists of three parts: initialisation, condition, and afterthought – Bergi Jul 09 '16 at 13:38
  • "*How do I make it so hello2(); is only called once at the end of hello();?*" - it already is. Sounds like you want to make it called only once per sequence of asynchronous `hello` calls. Just add an `else` in front of the `hello2();` for that. – Bergi Jul 10 '16 at 13:31
  • @Bergi I just tried this and updated my [JSFiddle](http://jsfiddle.net/hrvt5dgb/1/). Still lists years 0-9 multiple times, I believe for each time the `hello()` is looped. – Tyler Jul 10 '16 at 13:34
  • @Bergi I only want `hello2()` to be called **once** which should happen **after** `hello()` has finished. – Tyler Jul 10 '16 at 14:08
  • Ohhhh! My problem is with `hello2();` which loop doesn't stopl.... – Tyler Jul 10 '16 at 14:57
  • your loop in hello2 end if t < f is false. So it ends when t is equal or bigger then f. Then you recall hello2 with: if (t > f) window.setTimeout(hello2, 0); – progysm Jul 10 '16 at 15:39
  • @TimMarshall you are using recursion, but you have a bad stop/exit condition. Also, in recursion, you need to pass variable initial values externally as function parameters/or as global variables depending on your scope: http://stackoverflow.com/questions/7065120/calling-a-javascript-function-recursively so which is your exit condition here `t>f` for which initial values: `f=TotalPension` and `t` is? – loretoparisi Jul 10 '16 at 15:40
  • @loretoparisi `t = 125000;` – Tyler Jul 10 '16 at 16:27

0 Answers0