JS to achieve the top and bottom symmetrical 99 multiplication table _javascript Skills

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.