2

I am trying to create a simple times table in a html document using Javascript. This is my code so far:

<!DOCTYPE>
    <html>
    <head>
        <title>Table</title>
    </head>
    <body>
    <script language="javascript" type="text/javascript">
        for (var a=0; a < 10; a++) {
            document.write("<tr>");
            for(var b=0; b<10; b++) {
                document.write("<td>"a*b"</td>");
        }
        document.write("</tr>");
        }
    </script>
    </body>
    </html>

I looked through the posted questions, but could not find an answer, possibly because I am a beginner programmer and did not understand most of it.

2
  • 1
    document.write("<td>" + (a*b) + "</td>");? Commented Feb 26, 2014 at 12:59
  • 1
    Indeed, you have to concatenate your strings properly using the + sign. Commented Feb 26, 2014 at 13:19

8 Answers 8

2

Well, first of all you should insert the tr (rows) and td (cells) into a table element... Something like

document.write("<table>");
// your loop here
document.write("</table>");

There are better ways to do this, though!

Sign up to request clarification or add additional context in comments.

1 Comment

Glad it worked! :) would you mind flagging the answer as the correct one please? It means a lot to me, thank you :)
2

<!Doctype html>
<html>
<head>
<script type="text/javascript">
function table()
{
	this.calcmul = calc;
}
 function calc(arg1,arg2)
 {
		var multi = (arg1 * arg2);
		return multi;	
 }
 var table2 = new table();  
</script>
</head>
<body>
<table border="solid 2px;" style="font-size:50px;">
	<thead><tr>
			<script>
			for(var j=1; j<=10; j++)
			{
				document.write("<th><label style='color:red;'>"+i+"</label></th>");
			}
			</script>
			</tr>
	</thead>
	<tbody>
		<script type="text/javascript">	
for(var i =1; i<=10; i++)
{
	document.write("<tr>");
	for(var k=1; k<=10; k++)
		{
			var arg1 = i;
			var arg2 = k; 
			document.write("<td>"+table2.calcmul(arg1,arg2)+"</td>");	
		}
	document.write("</tr>"); 
}	
</script>
	</tbody>
</table>
</body>
</html>

Comments

1

use '+' to concate.

<!DOCTYPE>
<html>
<head>
<title>Table</title>
</head>
<body>
<script language="javascript" type="text/javascript">
for (var a=0; a < 10; a++) {
document.write("<tr>");
for(var b=0; b<10; b++) {
document.write("<td>"+(a*b)+"</td>");
}
document.write("</tr>");
}
</script>
</body>
</html>

Comments

0
var students = [`Ahsan`,`Ali`,`Moiz`,`Raza`,`Zia`];
var rollNums = [10,20,30,40,50];
document.write(`<table>
<th>Student Name</th>
<th>Roll Num</th>`)
for (var i = 0 ; i < students.length ; i++){
    document.write(`
    <tr>
        <td>${students[i]}</td>
        <td>${rollNums[i]}</td>
    </tr> <br />`)
};
document.write(`</table>`)

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

This might be another way of creating table using Vanilla Javascript without any tag

var table = document.createElement('table')
table.setAttribute('border', 1) // optional styling
var body = table.createTBody()

for (var a = 0; a < 10; a++) {
  var row = body.insertRow(a)
  for (var b = 0; b < 10; b++) {
    row.insertCell(b).innerText = (a + 1) * (b + 1)
  }
}

document.body.appendChild(table)
<!DOCTYPE>
<html>
  <head>
    <title>Table</title>
  </head>
  <body></body>
</html>

Comments

-2

<!DOCTYPE html>
<html>
<head>
<script>
 function calc(arg1,arg2)
 {
		var multi = (arg1 * arg2);
		return multi;	
 }
</script>
</head>
<body>
<table border="solid 2px;" style="color:black;font-size:50px;">
	<thead><tr>
			<script>
			for(var j=1; j<=10; j++)
			{
				document.write("<th>"+i+"</th>");
			}
			</script>
			</tr>
	</thead>
	<tbody>
<script>
for(var i =1; i<=10; i++)
{
	document.write("<tr>");
	for(var k=1; k<=10; k++)
		{
			var arg1 = i;
			var arg2 = k; 
			document.write("<td>"+calc(arg1,arg2)+"</td>");	
		}
	document.write("</tr>"); 
}	
</script>
	</tbody>
</table>
</body>
</html>

Comments

-2
<div id="yr" class="year"></div>
function year(){
    var test = '<table border="1px"><thead><tr><th><</th><th colspan="2">2015-2016</th><th>></th><tr></thead><tbody>';
    var tr='';
    for(var i=0;i<4;i++){
        tr += '<tr>';
        for(var j=0;j<4;j++){
            tr += '<td>'+2015+'</td>';
            }
        }
        tr +='</tr>';
    test += tr;
    return document.getElementById('yr').innerHTML = test;
}
year();

1 Comment

Fixed rows and columns
-3

<!DOCTYPE html>
<html>
<head>
<script>
 function calc(arg1,arg2)
 {
		var multi = (arg1 * arg2);
		return multi;	
 }
</script>
</head>
<body>
<table border="solid 2px;" style="color:black;font-size:50px;">
	<thead><tr>
			<script>
			for(var j=1; j<=10; j++)
			{
				document.write("<th>"+i+"</th>");
			}
			</script>
			</tr>
	</thead>
	<tbody>
<script>
for(var i =1; i<=10; i++)
{
	document.write("<tr>");
	for(var k=1; k<=10; k++)
		{
			var arg1 = i;
			var arg2 = k; 
			document.write("<td>"+calc(arg1,arg2)+"</td>");	
		}
	document.write("</tr>"); 
}	
</script>
	</tbody>
</table>
</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.