Home > Web Front-end > JS Tutorial > JavaScript Fun Question: Bouncy Ball

JavaScript Fun Question: Bouncy Ball

黄舟
Release: 2017-02-13 16:05:52
Original
1454 people have browsed it

A naughty kid is playing with a bouncy ball on a tall building that is h meters high. h is measured in meters.

Then, he threw the bouncy ball, and the ball quickly rebounded to the height of the original bounce, such as 2/3 two-thirds.

His mother was currently at a height of window, looking out the window. window should be smaller than h, that is to say, His mother was definitely beneath him.

Excuse me, how many times can this mother see the ball in front of the window? Includes both decline and rebound.

Note:

Only if the height of the ball’s rebound is absolutely greater than window, this mother can see it.

Example:

h = 3, bounce = 0.66, window = 1.5, the result is 3

h = 3 , bounce = 1, window = 1.5, the result is -1

If there is an abnormal value here, -1 will be returned.

What kind of situation is considered an outlier?

1.bounceBounce rate<=0 or >=1, such a situation is obviously impossible

2. The height of the naughty child is lower than his mother, so it is meaningless. Return directly to -1

Okay, let’s take a look at the idea:

1. Under normal circumstances, the mother will see the bouncy ball at least once when it falls for the first time.

2. If the height of the rebound is greater than window, then 2 times will be added each time it goes up and down.

Well, first eliminate the outliers, and then write according to the above ideas:


function bouncingBall(h,  bounce,  window) {
    var total = -1;
    if(bounce <= 0 || bounce >= 1){
        return total;
    }
    if(h > window){
        total = 1;
        var current = h * bounce;
        while(current > window){
            total += 2;
            current = current * bounce;
        }
    }
    return total;
}
Copy after login


The above is the content of JavaScript interesting question: bouncy ball , for more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template