JavaScript (language script), for loop practice

Source: Internet
Author: User

i++; equivalent to i=i+1;
++i; equivalent to i=i+1;

Example One,

var a=0;
var b=a++;
var b =a;
a=a+1;
alert (b);


a++ give a B, b=0, and then + +
++a + +, then assign a to B

for (Var i=0;i<10;i++)
{
alert (i);
}
Perform initial value first-condition judgment--loop body--state change--condition judgment--loop body--State change ...


example Two, an odd number and an even number within 100 input

var odd = "";
var even = "";

for (Var i=1;i<100;i++)
{
if (i%2==0)
{
even = even +i+ "|";
}
Else
{
Odd = Odd +i+ "|"
}
}
alert (even);
alert (odd);


example Three, take 100 or 7 related numbers
var s= "";
for (i=0;i<100;i++)
{
if (i%7==0 | | i%10==7 | | parseint (I/10) ==7)
{
s=s+i+ "|";
}
}

alert (s);


example Four,Sum of numbers within 100
var a=0;
for (Var i=0;i<100;i++)
{
A=a+i;
}
alert (a);


example Five,Ask for factorial within 10
var a=1;
for (i=1;i<10;i++)
{
A=a*i;
}
alert (a);

example Six,Output multiplication Table
var a= "";
for (var i=1; i<10; i++)
{
for (var b=1; b<10; b++)
{
if (b>=i)
{
var s=i*b;
a=a+i+ "X" +b+ "=" +s+ "|";
}
}
}
alert (a);


Homework One,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 hd=0.0001;
var cs=0;
while (true)
{
cs++;
HD =hd*2;
if (hd>8848)
{
Break
}
}
Alert (CS);


Homework Two,Badminton Racket 15 Yuan, the ball 3 yuan, the water 2 yuan. $200 each at least one, how much could it be
15*13=200;3*66=200;2*100=200;
X y Z
var kn= "";
var cs=0;
for (Var x=1;parseint (x*15)<=200;x++)
{
for (Var Y=1;parseint (y*3)<=200;y++)
{
for (Var z=1;parseint (z*2)<=200;z++)
{
cs++;
kn=kn+x+ "multiplied by" +15+y+ "multiplied by" +3+z+ "multiplied by" +2+ "less than" + "equals" +200;
if (x*15+y*3+z*2>200)
{
Break
}
}
}

}
Alert (CS);
Homework Three,

Pooled 1,2,5 20 Yuan money how many possible
1*20=20;2*10=20;5*4=20;
X y Z
var kn= "";
var cs=0;
for (Var x=1;x*1<=20;x++)
{
for (Var y=1;y*2<=20;y++)
{
for (Var z=1;z*5<=20;z++)
{
cs++;
kn=kn+x+ "multiplied by" +1+y+ "multiplied by" +2+z+ "multiplied by" +5+ "less than" + "equals" +20;
if (x*1+y*2+z*5>20)
{
Break
}
}
}
}
Alert (CS);

JavaScript (language script), for loop practice

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.