JS for Loop Small example

Source: Internet
Author: User
Tags soap ming

1, the output of 1-100 and

var sum = 0;              for (var i=1;i<=100;i++) {                = sum + i;            }            document.write (sum);

2, output 1-100 odd and

var sum = 0
for (var i=1;i<=100;i+=2) { = sum+i; } document.write (sum);

3, output 1-100 even-numbered and

var sum = 0;              for (var i=2;i<=100;i+=2) {                = sum+i;            }            document.write (sum);

4. Printing 99 multiplication Tables

varI//used to control the outer loop variable, which means looping a few lines            varJ//used to control the inner loop variable, which represents the loop of several columns            varNum =0; document.write ("<table border= ' 0 ' cellspacing= ' 0 ' cellpadding= ' 0 ' >");  for(i=9;i>=1;i--) {document.write ("<tr>")                 for(j=1;j<=i;j++) {num= i*J; document.write ("<td>" +i+ "*" +j+ "=" +num+ "</td>"); } document.write ("</tr>"); } document.write ("</table>");

5, Rooster 5 Yuan A, hen 3 yuan A, chicken 1 yuan Three only, ask 100 yuan just spend and want to buy 100 chickens, can buy each kind of chicken how many only?

//count indicates how many kinds of buying method            varCount =0; //How many cocks can i buy for 100 dollars?             for(vari=0;i<=20;i++) {//How many hens can I buy for 100 yuan?                 for(varj=0;j<=33;j++) {//How many chickens can I buy for 100 dollars?                     for(varx=0;x<=100;x++) {//condition to judge, three kinds of chickens add up enough 100, and spend money equal to 100 yuan                        if((i+j+x==100) && (5*i+3*j+x/3==100)) {count++; document.write ("Rooster:" +i+ "hen:" +j+ "chick:" +x); document.write ("<br/>"); } }}} document.write ("<br/>"); document.write (count);

6, 1,2,5 to gather enough 20 yuan how many ways?

var count=0;              for (i=0;i<=4;i++) {                for (j=0;j<=10;j++) {                    for (k=0;k<=20;k++ {                        if(i*5+j*2+k*1==20) {                            count+ +;                            document.write ("Five Yuan:" +i+ "Zhang" + "two Yuan:" +j+ "one Yuan:" +k);                            document.write ("<br/>");            }}} document.write (count);

Number of daffodils within 7 and 1000

var a,b,c;              for (var i=100;i<1000;i++) {                a=parseint (i/100);                    B=parseint ((i-a*100)/10);                C=parseint (i-a*100-b*10);                 if (a*a*a+b*b*b+c*c*c==i) {                       document.write ("Narcissus number:" +i+ "<br/>");}            }

8, there is a pair of young rabbits, one months later as a small rabbit, rabbit one months later long as a rabbit, into a rabbit will give birth to a pair of rabbits, asked 10 months after the number of rabbits? (Rabbits will not die)

//          let's start with a math formula before we begin .//YT XT Ct//1 0 0//0 1 0//1 0 1//1 1 1//2 1 2//based on the above results, we can get a formula.//ct=ct+xt; Every month the rabbit equals the rabbit of the month and the bunny of the month.//Xt=yt; Every month the rabbit is equal to the rabbit of last month//yt=ct; Every month the hare is equal to the rabbit of the last month.            varct=0;//into a rabbit            varxt=0;//Little Bunny .            varYt=1;//Young Rabbits            varm = 10;//Month            varsum=0;//logarithmdocument.write ("There are a pair of young rabbits, one months later as a small rabbit, the rabbit one months later into a rabbit, into a rabbit will give birth to a pair of rabbits, asked 10 months after the number of rabbits?" "+" <br/> ");  for(vari=1;i<=m;i++){                //January is a special month, the rabbit only a rabbit, this month is an initial value.                if(i==1) {CT=0; XT=0; YT=1; Sum=ct+xt+YT; }Else{CT=ct+XT; XT=YT; YT=CT; Sum=ct+yt+XT; } document.write ("Into the Rabbit has:" +ct+ "Pair &nbsp;&nbsp; Rabbit has:" +xt+ "pairs &nbsp;&nbsp; young rabbits have:" +yt+ "pairs &nbsp;&nbsp; total logarithm:" +sum+ "< Br/> "); }            

9, The Big Horse Pack 2 stone grain, the horse carries 1 stone grain, two ponies carries one stone grain, must use 100 horse, carries 100 stone grain, how to deploy?

//Calculate how many horses are needed for 100 stone grain per stud
varCount=0;
The horse needs 50 horses . for(i=0;i<=50;i++){
The Chinese horse needs 100 horses. for(j=0;j<=100;j++){
The pony needs 200 horses, but it says it uses up to 100, so this is for(k=0;k<=100;k++){ if((i+j+k==100) && (2*i+1*j+k/2==100)) {count++; document.write ("Big horse:" +i+ "in the Horse:" +j+ "Pony:" +k); document.write ("<br/>"); }}}} document.write (Count+ "<br/>");

10. Print out the following graphic







var b;             // Number of rows to loop             for (var a=1;a<=7;a++) {                // loop number of columns for                (b=1;b<=8;b++ ) {                    document.write ("");                }                 // cycle through each line output a line break                document.write ("<br/>");            }

11. Print out the following graphic





 for (var a=1;a<=5;a++) {                  for (var b=1;b<=a;b++) {                      document.write ( "");                document.write ("<br/>");            }

12. Print out the following graphic





 for (var a=1;a<=5;a++) {  //1for                (var b=5;b>=a;b--) {        //5                    document.write ("");                }                document.write ("<br/>");            }

13. Print out the following graphic


---
--
-

     for (var a=1;a<=5;a++) {                  for (var b=5;b>=1;b--) {                        //  Loop to the output block if the value of a is less than the value of B, otherwise the output circle                    if(a<b) {                        document.write ("-") ;                                            } Else {                         document.write ("");                    }                }                document.write ("<br/>");            }

14, Xiao Ming unit sent 100 yuan shopping card, Xiao Ming to the supermarket to buy three types of washing supplies, shampoo (15 yuan), Soap (2 Yuan), toothbrush (5 yuan). To spend 100 yuan good, but if you have a combination of purchase?

var sum = 0;          for (var i=0;i<=6;i++) {for                         (var j=0;j<=20;j++) {for                           (var k=0;k<=50;k++)                        { If((15*i+5*j+2*k==100)) {                        document.write ("Shampoo:" +i+ "&nbsp;&nbsp; Toothbrush:" +j+ "&nbsp; &nbsp; soap: "+k+" <br/> ");                        Sum++        ;        }}}} document.write (sum+ "<br/>");

15, five children lined up in a team. Ask the first how old, the first to say two years older than the second, ask the second, the second one is two years older than the third, and so on. Ask the fifth child how old, the fifth child said 3 years old. Q How old is the first child?

var i=1;          for (var a=1;a<=5;a++) {
Each child more than the previous child 2 years old i=i+2; document.write (i+ "<br/>"); }

16. There is a monkey and a bunch of peaches in the park, and the monkeys eat half of the peaches every day and throw away a bad one in half. By the seventh day, the monkey opened his eyes and found that there was only a peach left. Q. How many are there in the park at first?

 //  First lists mathematical formulas  //  7 6 5 4  //   1 (UP) * (4+1) * (10+1) *  //   Last Peach number T  var  T=1 //             cycle 6 days, because seventh day it didn't eat  for  (var  i=1;i<7;i++) { //  The peach per day is equal to (t+1) * *  t= (t+1) *2; } document.write (t);  

17. A reconnaissance team received an urgent task requiring a number of persons to be selected as many as possible in a, B, C, D, E and F six players, subject to the following restrictions:

A and B at least one person;
A and D cannot go together;
A, E and F three shall send two men;
B and C neither go nor go;
C and D two to one;
If D does not go, then E will not go.
How many people should I ask for?

//0 Delegates do not go, 1 delegates go, they do not go first, and then each judge them to go            varA=b=c=d=e=f=0; //List Mathematical formulas//a+b=>1;//a+d!=2;//a+e+f==2;//b+c==2 | | b+c==0;//c+d==1;//d+e==0 | | d==1;//each value less than 2 means that they will execute the loop and will go to             for(a=0;a<2;a++){                    for(b=0;b<2;b++){                     for(c=0;c<2;c++){                         for(d=0;d<2;d++){                             for(e=0;e<2;e++){                                 for(f=0;f<2;f++){                                    //According to the formulas listed above, all are added to the judgment, and the conditions are the final result.                                    if(a+b>=1 && a+d!=2 && a+e+f==2 && b==c && c+d==1 && (d+e==0 | | d==1) {document.write (a,b,c,d,e,f+ "<br/>");                }                                }                            }                        }                    } }            }

The above is in order to better understand the for loop to write a small example, I hope to learn from you help ~

JS for Loop Small example

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.