Several small exercises about the for loop, such as odd and even numbers, factorial, summation, and odd factorial
1. odd and even numbers less than 100
Var js = "";
Var OS = "";
For (var I = 1; I <101; I ++)
{
If (I % 2 = 0)
{
OS = OS + "" + I;
}
Else
{
Js = js + "" + I;
}
}
Alert (OS );
Alert (js );
2. Take 7-related numbers of less than 100.
Var x = "";
For (var I = 0; I <101; I ++)
{
If (I % 7 = 0 | I % 10 = 7 | parseInt (I/10) = 7)
{
X = x + "" + I;
}
}
Alert (x );
3. Calculate the sum of numbers less than 100
Var sum = 0;
For (var I = 0; I <101; I ++)
{
Sum + = I;
}
Alert (sum );
4. Calculate the factorial of less than 10.
Var str = "";
For (var I = 1; I <11; I ++)
{
For (var j = I; j <= 9 ;)
{
Str = str + "" + I + "*" + j
}
}
Alert (s );
5. The height of basketball falls from a 1-meter-high place. The height of each bounce is 40%. After several rounds, the height of basketball is 0.1.
Var h = 1;
Var n =;
While (true)
{
H = h * 0.4;
If (h <0.1) {break}
N ++
}
Alert (n );
6. There is a checkerboard with 64 squares. Put one grain of rice in the first place, two grains of rice in the second place, four grains of rice in the third place, and eight grains of rice in the fourth place.
Var sum = 0;
For (var n = 1; I <65; n ++)
{
Var ds = 1;
For (var I = 1, I <= n-1; I ++ ){
Ds = ds * 2
}
Sum = sum + ds
}