javascript-page Print square, various triangles and diamonds

Source: Internet
Author: User

A. Square

A) control in the first for loop, outputting a total of a few lines. Depending on the output one line at a time, a newline character will be output later <br>;

b) control the output of several pentagram per row in the second for loop. In this way, you can print 10 pentagram in each row.

c) When i=0, enter the first loop, and then assign a value of 0 to J, it enters the second loop and starts printing. After printing 10, end the second for loop and print a newline character. At this point the first line of the pentagram is printed.

d) and so on, print 10 rows. Because each line prints 10, a square is formed.

Note: Why less than 10, instead print 10?

Because when the i=0, when the printing, 9, also printed. 0-9 is a 10 digit.

           for (var i = 0; i < i++) {                   for (var j = 0; J <; J + + ) {                       docume Nt.write ("☆");                   }                   document.write ("<br/>")               }

Two. Triangle _ lower left.

A) Similarly, the first line controls how many lines are printed. Still print 10 lines, I value is 0-9;

b) J's judging condition is j<=i,j used to control how many printed, when I equals 0, J set the condition only when the first j=0, you can only print one. Then the line break is printed, and when I equals 1, J has two, 0 and 1, and the second line prints two.

c) It also says that the number of prints per line is equal to the number of rows in the row. For example: The first line will print 1, the second line will print two. And so on.

d) The difference between j<=i and j<i: If the judging condition is changed to J<i, then each line will print the last one less. For example, in the first line, the i=0;j is also equal to 0; the first line prints only one line break. In the second row, the j= 0 can print a pentagram, but when the j=1, there is no way to print, you can only print 1. This will print a triangle that is less than the previous line.

         for (var i = 0; i < i++) {                   for (var j = 0; J <= I; j + +) {                       DOCUMENT.W Rite ("☆");                   }                   document.write ("<br/>");               }

Three. Triangle-upper Left

A) The value of I can only be reduced from 10, because we want to take 10 crosses, if the judging condition is >0, then 0 of the value can not be taken, only a range of 10-1. In this case, you can still print out 10 lines.

b) because we want to print the triangle at the top left, the number of triangles is reduced from top to bottom. The number of prints per line is controlled by J. Because the number of prints per row is decreasing, just in order to fit I.

c) The first line prints 10, the second line prints 9. J is starting from 0, as long as it is less than the value of I, you can form this order. For example, when i=10, the first line, J starting from 0, 0-9 printing 10; When i=9, the second, J is 0-8, and so on.

D) until the last line, I=1; then only J equals 0 can print one.

e) The difference between j<i and j<=i, if we change the condition of J to j<=i, the meaning is that when J equals the value of I will also print a, the first row I of the value of 1-10;j will become 0-11, Print 11. And so each line will print one more. When you print to the last line, that is, I equals 1, the value of J is 0, 1, and two is printed. But the value of I has reached the end, but it prints two stars. This is an error.

f) Let j<=i, can only let the value of J starting from 1, then the number of J value can be equal to I.

            for (var i = ten; i > 0; i--) {                   for (var j = 0; J < i; j + + ) {                       document . Write ("☆");                   }                   document.write ("<br/>");               }

Four.. triangle-Top Right

A) If you want to print the triangle at the top right, we need a space to squeeze it. Because, printing is gradual from left to right. We can print out a square first, and then determine the location of the print, decide whether to print stars or spaces.

b) So the value of I is still from 10-1; print the line; then the value of J is also printed from 10-1 to 10. In fact, every line is printed, but only in the absence of a line to print the number of stars are not the same. For example, in the first line we print 10 stars without printing a space. In the second row, Let's print a space before printing 9 stars.

c) The first line: i=10, enter J. Then J is also 10 and will enter judgment J<=i. Because J is decremented, and J has a maximum value of 10, the value of I is 10. The first line will print 10 stars.

D) The second line: I=9, when the first j=10, a space will be printed, and then the value of J will be 9-1, will be printed as a star.

e) The last line, the value of I is 1;j at 10-2, will print 9 spaces, and finally J is equal to 1 when printing a star.

f) If the judging condition is changed to J<i, then each line will print less than one star. The first star in each row is removed. The last line will only print spaces.

          for(vari = 10; i > 0; i--) {//controls the number of rows.                    for(varj = 10; J > 0; j--) {                       //the number of prints per line, each line to print, first printing is All Stars.                       //then, gradually print the number of spaces in front, reducing the number of stars behind.                       if(J <=i) {document.write (☆); }Else{document.write ("&nbsp;")}} document.write ("<br/>")

Five. Triangle-Right down

A) The method is similar to the top right. Each line is printed, but the order of printing is different, first print the space, and then print the stars.

b) Similarly, in the first line, i=10; Enter the J Loop and print 10. For interpretation, the value of j<=i,j is increased from 1-10. When J takes value 1-9, it is a blank space, and the star is printed only when J takes a value of 10.

c) In the second row, I is the value of 9, then J value from 1-8 when the print space, 9,10 when the stars.

D) in the last row, the value of I is 1,j from 1-10, which is >=i, so it will print as a star.

                for(vari = 10; i > 0; i--) {//controls the number of rows.                    for(varj = 1; J <=10; J + +) {                       //the number of prints per line, each line to print, first printing is All Stars.                       //then, gradually print the number of spaces in front, reducing the number of stars behind.                       if(J >=i) {document.write (☆)                       } Else{document.write ("&nbsp;"); }} document.write ("<br/>")               }

Six. Diamond

A) when the diamond is printed, it is divided into two parts.

b) The above section is 4 rows. The following section is three lines, which make up a diamond.

c) The above section is composed of:

First line: Three spaces, one star.

Second line: Two spaces, three stars.

Line three: A space, five stars.

The third line; no spaces, seven stars.

D) In fact, when we print two for loop, the second line prints the number, that is, J of the judging condition, j is less than a few to print a few. That way you can see. In the first row, the hollow lattice is 3,i 0. The space is 2,i to 1. Is 3-i. So the judging condition is 3-i.

e) The same method of discovering the relationship between stars and I. Write it as a condition to know how many things you need to print in this line.

f) The following sections are similar in composition:

First line: A space, five stars.

Second line: Two spaces, three stars.

Line three: A space, five stars.

                for(vari = 0; I < 4; i++) {                    for(varj = 0; J < 3-i; J + +) {document.write ("&nbsp;")                   }                    for(k = 0; k < 2*i+1; k++) {document.write (☆)} document.write ("<br/>")               }                for(vari = 0; I < 3; i++) {                   //Print Space                    for(varj = 0; J <= I; J + +) {document.write ("&nbsp;")                   }                   //Print stars                    for(vark = 0; K < 5-i*2; K + +) {document.write (☆)} document.write ("<br/>")               }

javascript-page Print square, various triangles and diamonds

Related Article

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.