js loop exercises

question:

1. The thickness of a piece of paper is 0.0001 meters. Fold the paper in half. How many times should the thickness exceed the height of Mount Everest, which is 8848 meters.
2. There is a pair of young rabbits, the young rabbits will grow into young rabbits after 1 month, and the young rabbits will grow into adult rabbits after 1 month and give birth to a pair of young rabbits. Ask how many pairs of rabbits there will be after 8 months. What are the pairs of rabbits and adult rabbits respectively?
3. Badminton racket 15 yuan, ball 3 yuan, water 2 yuan. 200 yuan at least one of each, how many possibilities
4. The ball falls to the ground from a height of 100 meters. Each time it hits the ground, it bounces up to half of its original height and then falls. How many meters did it pass when it fell for the 10th time? How high did the 10th bounce go?
5. The monkey eats peaches: How many peaches are there on the first day, half of all existing peaches are eaten every day plus one more, and only one is left on the 10th day, how many peaches are there in total on the first day?

answer:

  1. The thickness of a piece of paper is 0.0001 meters, fold the paper in half, how many times the thickness is more than the height of Mount Everest 8848 meters.
var h = 0.0001;
var i = 0;
while(true){
    
    
    h = h * 2;
    i++;
    if(h > 8848){
    
    
        break;
    }
}
console.log(i);
  1. There is a pair of young rabbits, the young rabbits will grow into young rabbits after 1 month, and the young rabbits will grow into adult rabbits after 1 month and give birth to a pair of young rabbits. Ask how many pairs of rabbits there will be after 8 months, young rabbits, small rabbits, How many pairs of rabbits are there.
var y = 8;
var baby = 1;
var yong = 0;
var adult = 0;
for (var i = 1; i <= y; i++) {
    
    
    if(i===1){
    
    
        baby=1;
        yong=0;
        adult=0;
    }else{
    
    
        var prev_baby = baby;
        var prev_yong = yong;
        var prev_adult = adult;
        baby = prev_adult + prev_yong;
        yong = prev_baby;
        adult = prev_yong + prev_adult;
    }
}
document.write("幼兔: " + baby + " 小兔: " + yong + " 成兔: " + adult + "<br>");
  1. The badminton racket is 15 yuan, the ball is 3 yuan, and the water is 2 yuan. 200 yuan at least one of each, how many possibilities
for(var i=1;i<200/15;i++){
    
    
    for(var j=1;j<200/3;j++){
    
    
        for(var k=1;k<200/2;k++){
    
    
            if(i*15 + j*3 + k*2 === 200){
    
    
                console.log("羽毛球拍"+ i +"个,球"+ j +"个,水"+ k +"个");
            }
        }
    }
}
  1. The ball falls to the ground from a height of 100 meters. Each time it hits the ground, it bounces up half of its original height and then falls. How many meters did it pass when it fell for the 10th time? How high did the 10th bounce go?
var h = 100;
var sum = 0;
for(var i=0;i<10;i++){
    
    
    h *= 0.5;
    sum += h;
}
console.log(sum);
console.log(h);
  1. The monkey eats peaches: How many peaches are there on the first day, half of all existing peaches are eaten every day plus one more, and only one is left on the 10th day, how many peaches are there in total on the first day?
var n = 1;
for(var i=0;i<10;i++){
    
    
    n = (n+1)*2;
    console.log(n);
}

Continuously updating...

Guess you like

Origin blog.csdn.net/weixin_41636483/article/details/116040823