简体   繁体   English

如何使用JavaScript为每个表格行打印不同的值

[英]How to print different value for each table row using javascript

I just want to create a multiplication table using Javascript but I don't want each row to have the same result. 我只想使用Javascript创建一个乘法表,但是我不希望每一行都有相同的结果。 I will picture that table I want to be created for my work 我将为工作创建想要创建的表的图片

In this code, the program will print 9 times 在此代码中,程序将打印9次

 for (a = 1; a <= 9; a++) { document.write('<div style= "float: left; margin: 25px">') for (i = 1; i <= 9; i++) { document.write(a + ' x ' + i + ' = ' + a * i + '</br>'); } } 

I already make a table for multiplication but I don't want to make all print 10 times. 我已经制作了一张用于乘法的表格,但我不想将所有文字打印10次。 I want to make different for each table cell 我想让每个表格单元格不同

I want to make like this picture and I'm new with JavaScript 我想像这张照片一样,并且是JavaScript的新手

在此处输入图片说明

 var times = 1; for (a = 9; a > 0; a--) { for (i = 9; i > 0 && i > (9 - times); i--) { document.write(a + ' x ' + i + ' = ' + a * i + ' '); } document.write('<br>'); times++; } 

You can simply add this to the second loop for (i = 1; i <= a; i++) that way you'll achieve what you want. 您可以简单地将其添加到for (i = 1; i <= a; i++)的第二个循环中for (i = 1; i <= a; i++)从而实现所需的目标。

 for (a = 1; a <= 9; a++) { document.write('<div style= "float: left; margin: 5px">') for (i = 1; i <= a; i++) { document.write(a + ' x ' + i + ' = ' + a * i + '</br>'); } } 

The simple answer is replace this line for (i=1; i<=9; i++) { with this: for (i=a; i<=9; i++) { 简单的答案是将for (i=1; i<=9; i++) {这一行替换for (i=1; i<=9; i++) {for (i=a; i<=9; i++) {

 for (a = 1; a <= 9; a++) { document.write('<div style= "float: left; margin: 25px">') for (i = a; i <= 9; i++) { document.write(a + ' x ' + i + ' = ' + a * i + '</br>'); } } 

For having the exact same thing as you posted in your picture, this code do the job : 为了使您拥有与图片完全相同的功能,此代码可以完成此工作:

 document.write("<table>"); for (a = 9; a > 0; a--) { document.write("<tr>") for (i = 9; i > a - 1; i--) { document.write('<td>' + a + ' x ' + i + ' = ' + a * i + '</td>'); } document.write('</tr>') } document.write("</table>") 

Note the use of decrementing loop to match the order of your picture and the usage of the table 请注意使用递减循环来匹配图片的顺序和表格的用法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM