As shown in title, the 99 multiplication table is almost always available in every technical language, not particularly rare. This article describes the two commonly used loops (for and while) in JavaScript to complete these four symmetrical 99 multiplication tables, an example of a good practice loop basis, which loops a table due to the untidy layout.
One, the lower left corner is a 90-degree trapezoid multiplication table:
For Loop code
1 document.write ("<table width= ' border=0 ' >"), 2 for (var i=1; i<=9; i++) {3 document.write ("< Tr> ") 4 for (Var j=1; j<=i; J + +) {5 document.write (" <td> "+ j +" * "+ i +" = "+ I*j +" </td> "); 6 }7 document.write ("</tr>"); 8 }9 document.write ("</table>");
While loop code
1 document.write ("<table width= ' border=0 ' >"); 2 var i = 1; 3 while (i<=9) {4 document.write ("<tr>"), 5 var j = 1; 6 while (j<=i) {7 document.write (" <td> "+ j +" * "+ i +" = "+ I*j +" </td> "); 8 J + +, 9 }10 document.write ("</tr>"), one i++;12}13 document.write ("</table>");
Example diagram
Second, the lower right corner is a 90-degree trapezoid multiplication table:
For Loop code
1 document.write ("<table width= ' border=0 ' >"); 2 for (var i=1; i<=9; i++) {3 document.write ("<tr>"), 4 for (var n=i; n<9; n++) {5 document.write ( "<td> </td>"); 6 } 7 for (var j=i; j>=1; j--) {8 document.write ("<td>" + j + "*" + i + "=" + I*j + "</td>"); 9 }1 0 document.write ("</tr>");}12 document.write ("</table>");
While loop code
1 document.write ("<table width= ' border=0 ' >"); 2 var i = 1; 3 while (i<=9) {4 document.write ("<tr>"), 5 var n = i, 6 while (n<9) {7 document.write ("< Td> </td> "); 8 n++; 9 }10 var j = i;11 while (j>=1) { document.write ("<td>" + j + "*" + i + "=" + I*j + "</ Td> "), j--;14 }15 document.write (" </tr> "), i++;17}18 document.write (" </table > ");
Example diagram
Three, the upper left corner of the 90-degree trapezoid multiplication table:
For Loop code
1 document.write ("<table width= ' border=0 ' >"), 2 for (var i=9; i>=1; i--) {3 document.write ("<tr> "); 4 for (Var j=1; j<=i; J + +) {5 document.write ("<td>" + j + "*" + i + "=" + I*j + "</td>"); 6 }7 document.write ("</tr> "); 8}9 document.write (" </table> ");
While loop code
1 document.write ("<table width= ' border=0 ' >"); 2 var i = 9; 3 while (i>=1) {4 document.write ("<tr>"), 5 var j = 1; 6 while (j<=i) {7 document.write (" <td> "+ j +" * "+ i +" = "+ I*j +" </td> "); 8 J + +, 9 }10 document.write ("</tr>"), one i--;12}13 document.write ("</table>");
Example diagram
Four, the upper right corner of the 90-degree trapezoid multiplication table:
For Loop code
1 document.write ("<table width= ' border=0 ' >"); 2 for (var i=9; i>=1; i--) {3 document.write ("<tr>"), 4 for (var j=8; j>=i; j--) {5 document.write ("<td> </td>"); 6 } 7 for (var j=i; j>=1; j--) {8 document.write ("<td>" + j + "*" + i + "=" + I*j + "</td>"); 9 }1 0 document.write ("</tr>");}12 document.write ("</table>");
While loop code
1 document.write ("<table width= ' border=0 ' >"); 2 var i = 9; 3 while (i>=1) {4 document.write ("<tr>"), 5 var j = 8; 6 while (j>=i) {7 document.write (" <td> </td> "); 8 j--; 9 }10 var j = i;11 while (j>=1) { document.write ("<td>" + j + "*" + i + "=" + I*j + "</ Td> "), j--;14 }15 document.write (" </tr> "), i--;17}18 document.write (" </table > ");
Example diagram
99 multiplication table with upper and lower left and right symmetry