js The logical thought of the circular statement in and python Similar to , It's just that the writing is different , Pay attention to the standardization of writing format .
One : seek 1-100 The sum of all primes between , And average value
var sum = 0;
var pingjun = 0;
for(var i = 0; i <= 100;i++){
sum = sum + i;
if (i == 100){
pingjun = sum /i;
}
}
document.write(" and “+sum + " average ”+ pingjun
Two : seek 1-100 The product of all numbers between
<script type="text/javascript">
var product = 1;
for(var i =1;i<= 100;i++){
product *= i;
# be careful *=
}
document.write(product);
</script>
Three ; seek 1-100 Between can be 3 All divisible numbers
<script type="text/javascript">
var sum = 0;
for(var i =0;i<= 100;i++){
if (i%3 == 0){
sum += i;
}
}
document.write(sum);
Four ; seek 1-100 Between all odd numbers Even sum
var jijisum = 0;
var dabosum = 0;
for(var i = 0;i<= 100;i++){
if(i%2 == 0){
dabosum += i;
}else{
jijisum += i;
}
}
document.write(jijisum,dabosum);
Five ; seek 200-300 Sum of all odd numbers between , requirement use continue
<script type="text/javascript">
var sum = 0;
for(var i > 200;i <= 300;i++){
if (i%2 == 0){
continue;
}
sum += i;
}
document.write(sum);
</script>
Six ; Realization of fibolache sequence 1 1 2 3 5 8 13 21 characteristic : Every first two numbers add up to make the third number , The first two The number is special , Start with the third number .
<script type="text/javascript">
var num1 = 1;
var num2 = 1;
var sum = 0;
for(var i=0;i<5;i++){
sum = num1 + num2;
num1 = num2;
num2 = sum;
}
document.write(sum);
sum = sum.toString();
document.write(sum);
</script>
Seven ; Calculate the return on bank deposits , principal 10000, The interest rate is three per thousand ,5 Earnings after year ( principal + interest )
<script type="text/javascript">
var money = 1000;
var sum_Money = sum_Money*1.003;
for(var i=0;i<=5;i++){
sum_Money *= 1.003
}
document.write(sum_Money);
</script>
Eight ; Print square * be careful : Value of initial value of loop ,i=0,i<5, loop 6 second , Printing 6 That's ok 6 column *
script type="text/javascript">
for(var i=0;i<=5;i++){
for(var j =0;j <= 5;j++){
document.write("*");
}
document.write("<br />");
}
</script>
Nine ; Print triangle *
*
**
***
****
<script type="text/javascript">
# stay for Loop defined variable , Internal can be used directly
var i =1;
var j =1;
for ( i=1;i<=4;i++){
for ( j=1;j<=i;j++){
document.write("*");
}
document.write("<br />");
}
</script>
*
**
***
****
<script type="text/javascript">
# Define the number of cycles loop_times, Increase code readability
var loop_times = 4;
for (var i = 1; i <= loop_times; i++){
for (var j = 1; j <= loop_times-i;j++){
document.write(" ");
}
for (var k= 0; k < i; k++){
document.write("*");
}
document.write("<br/>");
}
</script>
var loop_times = 4;
for (var i = 1; i <= loop_times; i++){
for (var j = 1; j <= loop_times-i;j++){
document.write(" ");
}
for (var k= 0; k < i; k++){
document.write("*");
}
document.write("<br/>");
}
</script>
Technology