JS small example for loop

Original link: https://www.mk2048.com/blog/blog.php?id=h0kabjk1ihaa&title=JS%E7%9A%84for%E5%BE%AA%E7%8E%AF%E5%B0%8F%E4 % BE% 8B% E5% AD % 90

1, the output of 1 to 100 and

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

 

2, the output of odd and 1-100

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

 

3, the output of the even and 1-100

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

 

4, 99 print multiplication tables

       var i;  //用来控制外层循环变量,表示循环几行
            var j;  //用来控制内层循环变量,表示循环几列
            var num =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, 5 yuan a rooster, a hen 3 yuan, one yuan three chicks, just ask 100 yuan spent, but also to buy 100 chickens, you can buy every breeder how many of each?

       //count表示有多少种买法
            var count =0;     
       //表示100元可以买多少只公鸡
            for(var i=0;i<=20;i  ){
          //表示100元可以买多少只母鸡
                for(var j=0;j<=33;j  ){
            //表示100元可以买多少只小鸡
                    for(var x=0;x<=100;x  ){
              //条件判断,三种鸡加起来够100只,并且花的钱等于100元
                        if((i j x==100)&&(5*i 3*j x/3==100)){
                            count  ;
                            document.write("公鸡:" i "母鸡:" j "小鸡:" x);
                            document.write("<br/>");
                        }
                    }
                }
            }
            document.write("<br/>");
            document.write(count);

 

6,1,2,5 lobbied how many ways there are twenty yuan?

       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("五元:" i "张" "两元:" j "一元:" k);
                            document.write("<br/>");
                        }
                    }
                }
            }
            document.write(count);

 

Number of daffodils within 7,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("水仙花数为:" i "<br />");
                  }
            }

 

8, a pair of young rabbits, a month later after a long bunny, bunny month as long as a rabbit, to the next pair of bunny rabbits will be born, how many rabbits will be asked after ten months? (Rabbits do not die)

//          开始前我们先列数学公式
//          yt                xt                ct
//          1                0                0
//          0                1                0
//          1                0                1
//          1                1                1
//          2                1                2
//          根据上面的结果我们可以得到一下公式
//          ct=ct xt;     每个月的成兔等于上个月的成兔加上本月的小兔
//          xt=yt;        每个月的小兔等于上个月的成兔
//          yt=ct;        每个月的幼兔等于上个月的成兔
            var ct=0;   //成兔
            var xt=0;    //小兔
            var yt=1;    //幼兔
            var m = 10;    //月份
            var sum=0;    //对数
            document.write("有一对幼兔,一个月后长为小兔,小兔一个月后长为成兔,成兔会生下一对小兔,问十个月后会有多少对兔子?" "<br/>");
            for(var i=1;i<=m;i  ){
                //一月份是一个特殊的月份,兔子只有一只幼兔,本月就是一个初始值.
                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("成兔有:" ct "对&nbsp;&nbsp;小兔有:" xt "对&nbsp;&nbsp;幼兔有:" yt "对&nbsp;&nbsp;总对数:" sum "<br/>");
            }
            

 

9, Malaysia pack 2 stone grain, medium grain horse pack 1 stone, two stone pony pack a food, use 100 horses, pack 100 stone food, how to deploy?

       //先算出100石粮食每种马需要多少匹
       var
count=0;
       //大马需要50匹
for(i=0;i<=50;i ){
          //中马需要100匹
for(j=0;j<=100;j ){
            //小马需要200匹,但是它说最多用100匹,所以这里是100
for(k=0;k<=100;k ){ if((i j k==100) && (2*i 1*j k/2==100)){ count ; document.write("大马:" i "中马:" j "小马:" k); document.write("<br/>"); } } } } document.write(count "<br />");

 

10, print out the following graphic

  ●●●●●●●●
  ●●●●●●●●
  ●●●●●●●●
  ●●●●●●●●
  ●●●●●●●●
  ●●●●●●●●
  ●●●●●●●●

       var a,b;
            //循环的行数
            for(var a=1;a<=7;a  ){
                //循环的列数
                for(b=1;b<=8;b  ){
                    document.write("●");
                }
                //循环完每一行输出一个换行
                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  ){  //1
                for(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--){    
                    //循环到当A的值小于于B的值时,输出方块,否则输出圆
                    if(a<b){
                        document.write("□");
                        
                    }else{ 
                        document.write("●");
                    }
                }
                document.write("<br />");
            }

 

14, Xiao Ming units made a $ 100 shopping card, Xiao Ming to the supermarket to buy three types of cleaning products, shampoo (15 yuan), soap (2 yuan), toothbrush ($ 5). Take 100 yuan spent the whole good, which can be combined with the purchase if?

     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("洗发水:" i "&nbsp;&nbsp;牙刷:" j "&nbsp;&nbsp;香皂:" k "<br />");
                        sum  ;
                    }
                }
            }
        }
        document.write(sum "<br />");

 

15, five children lined up. The first asked how old, said the first two years older than the second, and asked the second, the second said two years older than the third, and so on. Q. How old is the fifth child, fifth child said 3 years old. The first child to ask how old?

     var i=1;
        for(var a=1;a<=5;a  ){
        //每一个孩子比上一个孩子多2岁 i
=i 2; document.write(i "<br />"); }

 

16, the park has a monkey and a bunch of peaches, monkey eat peaches every day half of the total, the remaining half of a bad throw. By the time the seventh day, opened his eyes and found only one monkey peach. I asked the park beginning how many?

//          首先列出数学公式
//          7      6             5         4
//          1    (1 1)*2      (4 1)*2     (10 1)*2
//          最后桃子的个数t
            var t=1;
//            循环6天,因为第七天它没有吃
            for(var i=1;i<7;i  ){
//            由公式得出每天的桃子等于(t 1)*2
                t=(t 1)*2;
            }
            document.write(t);

 

17, a reconnaissance team received an urgent task, requiring A, B, C, D, E, F six players as much as possible to pick a number of people, with the following restrictions:

  ● A and B both go at least one person;
  ● A and D can not go together;
  ● A, E and F in the three to send two people to go;
  ● B and C go or not to go;
  ● C and D two people go to a;
  ● D if not, then E does not go.
  Q. What should let a few people go?

//0代表不去,1代表去,他们首先都不去,然后挨个判断他们去
            var a=b=c=d=e=f=0;  
//          列出数学公式
//          a b=>1;
//          a d!=2;
//          a e f==2;
//          b c==2 || b c==0;
//          c d==1;
//          d e==0 || d==1;
//          每个值小于2表示他们都会执行循环,都会去
            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  ){
                                    //根据上面列出的公式,全都加到判断中,符合条件的就是最终的结果
                                    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 />");
                                    }
                                }
                            }
                        }
                    }
                }
            }

 

    These are small examples in order to better understand the for loop written, we hope to help learning ~


More professional front-end knowledge, make the [2048] ape www.mk2048.com

Guess you like

Origin blog.csdn.net/whiteGay/article/details/102769420