Write a program that creates a text file that contains a power of two table in HTML format. The file could be called "twoPowerTable.html". When it is viewed with a browser you will see something like:

Power of 2 Value
0 1
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256
9 512
10 1024

The file should start with something like:

<html><head>
<title>Powers of Two</title>
</head>
<body>
<table border cellpadding=5>
<tr><th>Power of 2</th><th>Value</th></tr>

And end with:

</table>
</body></html>

Each line of the table looks like:

<tr><td>0</td><td>1</td></tr>

I don't know why it can't show the table but i am doing the Exercises in this website: http://chortle.ccsu.edu/java5/Notes/chap83/progExercises83.html

Recommended Answers

All 3 Replies

I guess this is a homework assigment? Well I will tell you how you can do it, but I will not write the code for you.

1. Create the document with the table (it needs to have a id attribute)
2. Write a function in JavaScript that calculates the powers of 2 and dynamically add the rows to the table.

You can learn about adding rows to a table here: http://www.w3schools.com/js/tryit.asp?filename=try_dom_table_insertrow

~G

yep! it's homework assignment! Anyway thx a lot though

Member Avatar for rajarajan2017

Solution:

<html>
<head>
<script type="text/javascript">
function insRow()
{
var temp=1;
for (i=1;i<=11;i++)
{
	var x=document.getElementById('myTable').insertRow(0);
	var y=x.insertCell(0);
	var z=x.insertCell(1);
	y.innerHTML=i-1;
	if (i==1) {
		z.innerHTML=eval(temp);
	}else{
		temp+=temp;
		z.innerHTML=eval(temp);
	}
}
}
</script>
</head>

<body>
<table id="myTable" border="1">
<tr align="center">
<td colspan="2">End of the Table</td>
<td></td>
</tr>
</table>
<br />
<input type="button" onclick="insRow()" value="Insert row">
</body>
</html>

I dont know how to insert after a row

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.