The 99 multiplication table can be implemented in many languages. This article describes two commonly used loops (for, while) in JavaScript to complete the four symmetric 99 multiplication tables, an example of a good practice for looping, which loops a table because of the ragged layout.
First, the lower left corner is the degree of trapezoidal multiplication table:
For Loop code
document.write ("<table width= ' border= ' >");
for (var i= i<=; i++) {
document.write ("<tr>");
for (Var j= j<=i; J + +) {
document.write ("<td>" + j + "*" + i + "=" + I*j + "</td>");
}
document.write ("</tr>");
}
While loop code
document.write ("<table width= ' border= ' >");
var i =;
while (i<=) {
document.write ("<tr>");
var j =;
while (j<=i) {
document.write ("<td>" + j + "*" + i + "=" + I*j + "</td>");
j + +;
}
document.write ("</tr>");
i++;
}
document.write ("</table>");
Sample diagram
Second, the lower right corner of the trapezoidal multiplication table:
For Loop code
document.write ("<table width= ' border= ' >");
for (var i= i<=; i++) {
document.write ("<tr>");
for (var n=i; n<; n++) {
document.write ("<td> </td>");
}
for (var j=i j>=; j--) {
document.write ("<td>" + j + "*" + i + "=" + I*j + "</td>");
}
document.write ("</tr>");
}
document.write ("</table>");
While loop code
document.write ("<table width= ' border= ' >");
var i =;
while (i<=) {
document.write ("<tr>");
var n = i;
while (n<) {
document.write ("<td> </td>");
n++;
}
var j = i;
while (j>=) {
document.write ("<td>" + j + "*" + i + "=" + I*j + "</td>");
j--;
}
document.write ("</tr>");
i++;
}
document.write ("</table>");
Sample diagram
Third, the upper left corner for the degree of trapezoidal multiplication table:
For Loop code
document.write ("<table width= ' border= ' >");
for (var i= i>=; i--) {
document.write ("<tr>");
for (Var j= j<=i; J + +) {
document.write ("<td>" + j + "*" + i + "=" + I*j + "</td>");
}
document.write ("</tr>");
}
document.write ("</table>");
While loop code
document.write ("<table width= ' border= ' >");
var i =;
while (i>=) {
document.write ("<tr>");
var j =;
while (j<=i) {
document.write ("<td>" + j + "*" + i + "=" + I*j + "</td>");
j + +;
}
document.write ("</tr>");
i--;
}
document.write ("</table>");
Sample diagram
Four, upper right corner of the degree of trapezoidal multiplication table:
For Loop code
document.write ("<table width= ' border= ' >");
for (var i= i>=; i--) {
document.write ("<tr>");
for (var j=; j>=i; j--) {
document.write ("<td> </td>");
}
for (var j=i j>=; j--) {
document.write ("<td>" + j + "*" + i + "=" + I*j + "</td>");
}
document.write ("</tr>");
}
document.write ("</table>");
While loop code
document.write ("<table width= ' border= ' >");
var i =;
while (i>=) {
document.write ("<tr>");
var j =;
while (j>=i) {
document.write ("<td> </td>");
j--;
}
var j = i;
while (j>=) {
document.write ("<td>" + j + "*" + i + "=" + I*j + "</td>");
j--;
}
document.write ("</tr>");
i--;
}
document.write ("</table>");
Sample diagram
The above content is in this article to share the JavaScript commonly used two kinds of loops (for, while) completes these four kind of symmetrical 99 multiplication tables, hoped that has the help to everybody!