Question
35
© 2008 Pearson Education, Inc. All rights reserved.
Fig. 8.6 | Compound interest calculation with a for loop (Part 1 of 2).
1 <?xml version = "1.0" encoding = "utf-8"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 <!-- Fig. 8.6: Interest.html --> 6 <!-- Compound interest calculation with a for loop. --> 7 <html xmlns = "http://www.w3.org/1999/xhtml"> 8 <head> 9 <title>Calculating Compound Interest</title> 10 <style type = "text/css"> 11 table { width: 100% } 12 th { text-align: left } 13 </style> 14 <script type = "text/javascript"> 15 <!-- 16 var amount; // current amount of money 17 var principal = 1000.0; // principal amount 18 var rate = .05; // interest rate 19 20 document.writeln( 21 "<table border = \"1\">" ); // begin the table 22 document.writeln( 23 "<caption>Calculating Compound Interest</caption>" ); 24 document.writeln( 25 "<thead><tr><th>Year</th>" ); // year column heading 26 document.writeln( 27 "<th>Amount on deposit</th>" ); // amount column heading 28 document.writeln( "</tr></thead><tbody>" ); 29
36
© 2008 Pearson Education, Inc. All rights reserved.
Fig. 8.6 | Compound interest calculation with a for loop (Part 2 of 2).
30 // output a table row for each year 31 for ( var year = 1; year <= 10; ++year ) 32 { 33 amount = principal * Math.pow( 1.0 + rate, year ); 34 document.writeln( "<tr><td>" + year + 35 "</td><td>" + amount.toFixed(2) + 36 "</td></tr>" ); 37 } //end for 38 39 document.writeln( "</tbody></table>" ); 40 // --> 41 </script> 42 </head><body></body> 43 </html>
Control variable year begins with a value of 1
Continue to execute the loop while year is less than or equal to 10
After each loop iteration, increase the value of year by 1