Home > Article > Web Front-end > JS realizes the symmetrical multiplication table of up, down, left and right_javascript skills
The multiplication table can be implemented in many languages. This article describes two commonly used loops (For, While) in JavaScript to complete these four symmetrical multiplication tables. This example is a good way to practice the basics of loops. Because the layout is not neat, a table is looped .
1. Trapezoidal multiplication table with the lower left corner in degrees:
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>");
Example image
2. The trapezoid multiplication table with degrees in the lower right corner:
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>");
Example image
3. Trapezoidal multiplication table with the upper left corner in degrees:
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>");
Example image
4. Trapezoidal multiplication table with the upper right corner in degrees:
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>");
Example image
The above content is the two commonly used loops (For, While) in JavaScript shared in this article to complete these four symmetrical multiplication tables. I hope it will be helpful to everyone!