1

I got undefined in the first iteration any comments is helpful to me thanks in advance

for (i = 0; i <= nummonth; i++) {
    var res = compound * (Math.pow(interest, i));
    res = res.toFixed(0);
  var tableHTML;
  var interestgained = (+res * interest)-res;
  interestgained = interestgained.toFixed(0);

  var powint = compound * (Math.pow(interest, i));

  powint = powint.toFixed(0);
  
  var tr = powint - compound;

  var ointerest = (interest - 1)*100;
  ointerest = ointerest.toFixed(0);
  
    tableHTML = tableHTML + "<tr><td>Compound: " + i + "</td><td> " + "$"+res + "</td><td> " + ointerest + "%"+"</td><td> " + "$"+interestgained + "</td><td>"+ "$"+powint +"</td></tr>";
  }
VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • *What* is `undefined`? Do you have a [mcve]? – VLAZ Dec 04 '21 at 13:14
  • 5
    Set `tableHTML` to `''` as initial value. – MinusFour Dec 04 '21 at 13:14
  • Note that if `nummonth` is the number of months, you should be going from `0` to `< nummonth`, not `<= nummonh`. What you have will do `nummonth` plus one months. Also, be sure to declare your variables (`i` looks undeclared in that code). – T.J. Crowder Dec 04 '21 at 13:18
  • already declared the i on for loop, I tried set < instead of <= but still the same result – eloy guides Dec 04 '21 at 13:46
  • @MinusFour yes I tried that but it displayed the last iteration only it doesn't display each iteration. – eloy guides Dec 04 '21 at 13:49
  • Thanks @MinusFour you got me! I put the tableHTML outside the for loop and have ' ' as value. all good!! – eloy guides Dec 04 '21 at 14:56
  • Welcome to SO. I see that you already have an answer. Before you ask another question, please read the [instructions](https://stackoverflow.com/help/how-to-ask). Then your questions will be beneficial not only to you but also to others. – David Lukas Dec 09 '21 at 12:04
  • 1
    @DavidLukas as I mentioned above I put the tableHTML outside the for loop the viewers to this post must understand the answer to elaborate more the answer is var tableHTML = ''; for{ // do the code here//} thats it! done – eloy guides Jan 28 '22 at 05:51

0 Answers0