Click here to Skip to main content
15,566,394 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<!DOCTYPE html>
<html>
    <h1></h1>

<table border ='1' cellpadding="2" cellspacing="2" style=";margin-left;">
  <tr>
    <!-- Create Headings for table -->
    <th bgcolor='cyan'>Year </th>
    <th bgcolor='cyan'>Amount on Deposit Dec 31</th>
  </tr>
  <!-- Create table -->
  <tr>
    <td id='year'></td> 
    <td id='amt'></td>
</table>

    <script src="script.js"></script>
    <link href="style.css" rel="stylesheet" type="text/css">
  </body>
</html>

-------------------------------------------------------------------------------------
//Javascript
var i = 0;
var msg1 = ' ';
var msg2 = ' ';
var rate = .05;
var principle = 1000.00;
var year = 1;

{
while (i < 10) {
msg1 += i + 1 + '<br />'; 
msg2 +='$' + principle * Math.pow(1.0 + rate, year + i++)  + '<br />';
}}
document.getElementById('year').innerHTML = msg1;
document.getElementById('amt').innerHTML = msg2;
document.writeln('<tr><td>' + )
(year % 2 !==0) 


------------------------------------------------------------------------------------
How it's supposed to look.
Imgur: The magic of the Internet[^]

What I have tried:

I've tried a few things, I know the table in html needs more rows but I'm not sure how to write that from JS and have it work properly.
Posted
Updated 10-Oct-21 23:35pm

1 solution

You need to use insertRow()[^] to insert rows into your table.
HTML
<table>
<thead>
    <tr>
        <th>Year</th>
        <th>Amount</th>
    </tr>
</thead>
<tbody id="tableBody">
</tbody>
</table>
JavaScript
function appendRow(yearNumber, amount){
    const tbody = document.getElementById("tableBody");
    const tr = tbody.insertRow();
    let td = tr.insertCell();
    let text = document.createTextNode(yearNumber);
    td.appendChild(text);
    td = tr.insertCell();
    text = document.createTextNode(amount);
    td.appendChild(text);
}
Demo[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900