JavaScript loops and Exercises

Source: Internet
Author: User
Tags what operator

Switch ()

{

Case

}

<script type= "Text/javascript" >    switch (a=3)    {case    1:    alert (a=1) Case    2:    alert (a=2) Case    3:    alert (a=3) Case    4:    alert (a=4) Case    5:    alert (a=5) Case    6:    alert (a=6) Case    7:    alert (a=7)     }</script>

Infinite loops

For (four elements: initial conditions, cyclic condition/judging condition, state change, loop body)

{

Loop body

}

<script type= "Text/javascript" > For    (var a=1;a<=10;a++)    {    alert (a)       }</script>

  

A=1 a loop 10 times last less than or equal to 10

Break jumps out of the loop

Continue jump out of the current loop

1. Height of Blue ball bounce

Basketball from 10 meters high place falls, each bounce height is the original 0.3 times times, asked to bounce 10 times after the height of the basketball.

<script type= "Text/javascript" >       var b=10 for       (var a=1; a<=10;a++)       {         b=b*0.3           }       Alert (b)   </script>

2. The board put food
The chessboard has 32 squares, the first lattice puts 1 sesame seeds, the second one puts 2, the third one puts 4, the fourth one puts 8 ... The weight of each sesame is 0.00001kg, if you want to fill the whole board, how much weight of sesame seeds required.

<script type= "Text/javascript" >       var a=1       var zhima=1 for       (var c=1;c<32;c++)       {       a=a*2;       Alert (a)       zhima=zhima+a             }       alert (Zhima)       var d=zhima*0.00001       alert (d)   </script >

3, 100 and 7 related to the number

<script type= "Text/javascript" > For    (var i=0;i<100;i++)    {        if (i%7==0| | i%10==7| | parseint ((i%10)) ==7)        {        alert (i)           }      }</script>

4 multiplication Formulas

<script type= "Text/javascript" > For    (var i=1;i<10;i++)    {for        (var j=1;j<10;j++)        {            if (j>i)            {            alert (i+ "*" +j)             }          }      }</script>

5. The thickness of a piece of paper is 0.0001 meters, fold the paper, fold how many times the thickness exceeds the height of Everest 8848 meters

var hou = 0.0001;var Cishu = 0;for (var i=0;i>=0;i++) {    hou = hou*2;    if (hou>8848)    {        cishu=i+1;        break;     }  } alert (Cishu);

6. There are a pair of young rabbits, 1 months after the young rabbit into a small rabbit, the rabbit 1 months later into a rabbit and gave birth to a pair of young rabbits, asked 8 months after how many pairs of rabbits, rabbits, rabbits, Cheng logarithm of how much respectively.

Bunny 1 Bunny 0 Rabbit 0 Initial value
Bunny 0 Bunny 10% Rabbit 0 first Month
Bunny 1 Bunny 0 Rabbit 1 second month
Bunny 1 Bunny 10% Rabbit 1 third Month
Bunny 2 Bunny 10% Rabbit 2 fourth Month
Bunny 3 Bunny 20% Rabbit 3 fifth month
Bunny 5 Bunny 30% Rabbit 5 sixth month

Current month Baby rabbit = last month's Rabbit + last month Bunny
Current month Bunny = Last month's Baby Bunny
Current month into rabbit = last month into rabbit + last month Bunny

var sy = 1;var SX = 0;var sc = 0; var ny = 0;var NX = 0;var NC = 0; for (Var i=0;i<2;i++) {         ny = SC+SX;        NX = sy;          NC = SC+SX;                 var sy = NY;    var sx = NX;      var sc = NC;       } Alert (NY); alert (NX); alert (NC);

7. Badminton racket 15 yuan, ball 3 yuan, water 2 yuan. $200 each at least one, how much could it be
Badminton racket up to buy 13 balls up to buy 66 water up to buy 100

var sum=0;for (Var i=1;i<14;i++) {for    (Var j=1;j<67;j++) {for        (var k=1;k<101;k++)        {            if ( i*15+j*3+k*2==200)            {                                 sum++;            }      }}  alert (sum);

8. Rooster 2, hen 1, chicken half, each of at least one, 100 buy 100 chickens how many possibilities
Cocks up to 50, hens up to 100, chick up to 200

var sum=0;for (Var i=1;i<51;i++) {for    (var j=1;j<101;j++)    {for        (var k=1;k<201;k++)        {            if (i*2+j*1+k*0.5==100 && i+j+k==100)            {                //document.write ();                sum++;}}}}    alert (sum);

9. Parentheses can only add or subtract, if you want to set the equation, what operator should be put in parentheses

for (Var a=-1;a<=1;a=a+2) {for (   var b=-1;b<=1;b=b+2) {for (       var c=-1;c<=1;c=c+2)       {           var d=-1;d<=1;d=d+2)           {               if (12+a*34+b*56+c*78+d*9==59)               {                   alert (A + "," +b+ "," +c+ "," +d ")//+,-, +,-               }                 }         }         } }    

  

 



JavaScript loops and Exercises

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.