JS loop + loop nesting + Classic examples + graphic questions

Source: Internet
Author: User

First, look at the characteristics of the loop nesting: the outer loop turns once, and the inner layer loops around.

In the previous essay, we introduced in detail the branching structure and the loop structure in JS, so we can briefly review the For loop structure:

1. The For loop has three expressions, respectively:

① Defining a Loop variable

② Judging Cycle conditions

③ Update the loop variable (between three expressions, separated by; )
For loop three expressions can be omitted, two;
2, for loop characteristics: First judge, then execute;
3, for the loop three expressions, can have multiple parts, separated by commas, but the second part of the judging conditions need to use && link, the final result needs to be true/false.

"Nested loop characteristics"
Outer loop control number of rows, inner loop controls the number of elements per row

[Do graphic problem thinking] (graphic questions see case three)
1, determine the graph altogether several lines, namely is the outer circle the frequency;
2, to determine that each line has several elements, representing a few inner layers of the loop;
3, determine the number of elements, that is, the number of cycles per inner layer;
Tips: Usually, find out the number of each element, the relationship with the line number, that is, the maximum value of the current inner loop (starting from 1 cycles)

Examples are as follows:

Case one:

Sum, implement 1+ (1+2) + (1+2+3) + (1+2+3+4) + (1+2+3+4+5) =35

The code is as follows:

1 var sum=0,sumrow=0; 2 for             (Var i=1;i<=5;i++) {3                 sumrow=0; 4                  5                 if (i!=1) document.write ("("); 6                  7 for                 (Var j=1;j<=i;j++) {8                     if (j!=i) document.write (j+ "+"); 9                     Else  document.write (j); 10                     Sumrow + = j;11                 }                 if (i==1) document.write ("+"), or                 if (i==5) document.write (") ="), or                 else document.write (") +");                 sum + = sumrow;17             }18             document.write (sum);

Case TWO:

Summation: Realizing 1!+2!+3!+4!+5!

Analysis
1 +
1*2+
1*2*3+
1*2*3*4+
1*2*3*4*5=

The code is as follows:

1 var sum=0;2 for             (var i=1;i<=5;i++) {3                 var jie=1;4 for                 (var j=1;j<=i;j++) {5                     Jie *= j;6                 }7                 sum + = jie;8             } 9             document.write ("1!+2!+3!+4!+5!=" +sum);

Case three: (six big graphic questions)

1. Rectangle

The code is as follows:

1 for (Var i=1;i<=5;i++) {2 for                 (Var j=1;j<=5;j++) {3                     document.write ("*"); 4                 }5                 document.write ("<br/>"); 6             }7             8             document.write ("

Implementation results:

2, right Triangle

The code is as follows:

1 for (Var i=1;i<=5;i++) {2 for                 (Var j=1;j<=i;j++) {3                     document.write ("*"); 4                 }5                 document.write (" <br/> "), 6             }7             8             document.write (" 

Implementation results:

3, Parallelogram

The code is as follows:

1 for (Var i=1;i<=5;i++) {2 for                 (Var j=1;j<=i+4;j++) {3                     if (j<i) document.write ("&nbsp;"); 4                     else{document.write ("*");} 5                 }6                 document.write ("<br/>"); 7             }8             9             document.write ("

Implementation results:

4. Diamond

The code is as follows:

1 for (Var i=1;i<=4;i++) {2 for                 (Var j=1;j<=4-i;j++) {3                     document.write ("&nbsp;"), 4                 } 5 for                 ( var k=1;k<=2*i-1;k++) {6                     document.write ("*"); 7                 } 8                 document.write ("<br/>"); 9             }10             for (Var i=1;i<=3;i++) {One for                 (var j=1;j<=i;j++) {                     document.write ("&nbsp;");                 }14 for                 (var k=1;k<=7-2*i;k++) {                     document.write ("*"),                 }17                 document.write ("< br/> ");             }19             document.write (" 

Implementation results:

5, Digital equilateral triangle

The code is as follows:

1 for (Var i=1;i<=4;i++) {2                 //Space 3 for                 (Var k=1;k<=4-i;k++) {4                     document.write ("<span style=" display:inline-block;width:8px; ' ></span> "); 5                 } 6                 var n = 1; 7                 //increment 8 for                 (Var j=1;j<=i;j++) {9                     document.write (n);                     n++;11                 }12< C12/>n-=2;13                 //decrement for                 (var m=1;m<=i-1;m++) {                     document.write (n);                     n--;17                 }18                 //Enter                 document.write ("<br/>"),             }21             document.write ("

Implementation results:

6, 99 multiplication table (printed in table Form)

The code is as follows:

1 document.write ("<table width= ' >") 2 for             (Var i=1;i<=9;i++) {3                 document.write ("<tr>") 4 For                 (Var j=1;j<=i;j++) {5                     document.write ("<td>" +i+ "*" +j+ "=" +i*j+ "</td>"); 6                 } 7                 document.write ("</tr>") 8             } 9             document.write ("</table>")             document.write ("<br/><br/><br/><br/><br/><br/><br/><br/><br /><br/><br/> ");

Implementation results:

Case FOUR:

Enter a number, and then determine whether it is a positive integer, if not, prompt for re-entry;

If yes, invert the number around and then output (ex: 12345, 54321 after flipping)

The code is as follows:

1 var sum=1;
for (Var i=1;i<=2;) {
var num = prompt ("Please enter a number ~");
if (num) >0&&parseint (num) ==parsefloat (num)) {
Break
}
}
/*
num = 12345
5 num%10;
4 num/10%10;
3 num/10/10%10;
......
*/
document.write ("The number you entered is:" +num+ "<br/>");
for (j=1;; J + +) {
Sum *= 10;
var FZ = parseint (num%sum/(SUM/10));
document.write (FZ);
if (num-sum<=0) {
Break
}
}

Implementation results:

Case FIVE:

Write a program that accepts up to 10 digits and then evaluates the sum of all the positive numbers.
The user can enter 999 to terminate the program, to count the number of positive numbers entered by the user, and to display the sum of these positive numbers.

Analysis:

① input non-numeric value, does not occupy one of 10 opportunities, but requires re-entry;
② input is a numeric value, and only the integer is accumulated;
Determine if a number: number (num) is not Nan, and the description is numeric
To determine a positive number: num >0
Determine a number as an integer: parseint (num) ==parsefloat (num);

The code is as follows:

1 var i=1,sum=0,n=0; 2 while                 (i<=3) {3                     var num = prompt ("Please enter a number, I will calculate for you all positive and ~"); 4                     if (num) {5                         if (num==999) {6 Break                         ; 7                        } 8                         Else if (num>=0) {9                             sum = sum + parsefloat (num),                             i++;11                             n++;12                         }13                         else if (num<0) {                             i++;15                         }16                     }17                 }18                document.write ("You enter a total of" +n+ "positive integers" + "<br/> Their and as:" +sum);

Case SIX:

Enter a certain day of the year, judging the day is the first of the year?

The code is as follows:

Method One:

1 for (Var i=1;i<month;i++) {2                 if (i==1| | i==3| | i==5| | i==7| | i==8| |i==10| | I==12) {3                     sum + = 4                 }else if (i==4| | i==6| | i==9| | i==11) {5                     sum + = 6                 }else if (i==28) {7                     sum + = 8                 } 9             }10             if (year%4==0&&year%100 !=0| | year%400==0) &&month>2) {One                 sum + = (day+1);             }else{13                 sum + = day;14             }15             document.write ("You have entered a date of" +year+ "-" +month+ "-" +day+ "<br/> is the" +sum+ "Day for that year);

Method Two:

 1 var year = parseint (Prompt ("Please enter Years:")); 2 var month = parseint (Prompt ("Please enter month:")); 3 var day = parseint (Prompt ("Please enter Date:")); 4 5/* Assume all are common year, February 28 days */6 var sum = 0;                     7 switch (month-1) {8 case 12:9 sum + = 31;10 Case 11:11                    Sum + = 30;12 case 10:13 sum + = 31;14 Case 9:15  Sum + = 30;16 case 8:17 sum + = 31;18 case 7:19 sum + =                  31;20 case 6:21 Sum + = 30;22 case 5:23 sum + = 31;24                 Case 4:25 sum + = 30;26 case 3:27 sum + = 31;28             Case 2:29 sum + = 28;30 case 1:31 sum + = 31;32 }33 if (year%4==0&&year%100!=0| |             year%400==0) &&month>2) {sum + = (day+1);}else{36 sum + = day;37 }38 document.write ("You have entered a date of" +year+ "-" +month+ "-" +day+ "<br/> is the" +sum+ "Day of the year);

Case Seven:

Suppose a simple ATM's withdrawal process is like this: first prompt the user to enter a password (password),
You can enter up to three times, more than 3 times to prompt the user "password error, please collect card" to close the transaction. If the user's password is correct,
Then prompt the user to enter the withdrawal amount (amount), the ATM machine can only output 100 yuan of banknotes, one time to take the minimum money requirements
100 yuan, up to 1000 yuan. If the amount entered by the user meets the above requirements, the print output user obtains the money,
Finally prompt the user "The transaction completes, please collect the card", otherwise prompts the user to reenter the amount.

Assuming that the user password is 111111, please program the implementation.

The code is as follows:

1 var isTrue = false, 2                 n = 1, 3  4 while             (n <= 3) {5                 var pwd = prompt ("Please enter user password ~"); 6                 if (pwd = = 1111 {7                     isTrue = true; 8 break                     ; 9                 } else {                     n++;11                     if (n > 3) {                         document.write ("Wrong password, please take card!") ;                     }14                 }15             }16             if (isTrue) {                 1) {                     var num = prompt ("Please enter withdrawal amount: (100~1000 yuan)")                     if (num% = = 0 && num >= 0 && num <=) {                         document.write ("Your withdrawal amount is" + num + "meta ~& Lt;br/> Transaction completed, please pick up the card! ");                         break;23                     } else {                         alert (" You enter illegal! Please re-enter! ")                     }26                 }27                 //Cyclic input amount Operation             
Other Links: https://www.cnblogs.com/LJYqq/p/6680938.html https://www.cnblogs.com/cyjfighting/p/8253424.html

JS loop + loop nesting + Classic examples + graphic questions

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.