-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbouncingBalls.js
More file actions
34 lines (27 loc) · 796 Bytes
/
bouncingBalls.js
File metadata and controls
34 lines (27 loc) · 796 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Kata: Bouncing Balls
// Difficulty: 6kyu
// https://www.codewars.com/kata/5544c7a5cb454edb3c000047
/*
Description:
A child drops a ball from height `h` meters.
The ball bounces to a fraction of its height (`bounce`) and passes a window (`window`) on the way down and up.
We return the number of times the mother sees the ball pass in front of her window.
Return -1 if:
- h <= 0
- bounce <= 0 or >= 1
- window >= h
*/
function bouncingBall(h, bounce, window) {
if (h <= 0 || bounce <= 0 || bounce >= 1 || window >= h) return -1;
let count = 1;
while ((h *= bounce) > window) count += 2;
return count;
}
/*
Example:
bouncingBall(3, 0.66, 1.5) ➜ 3
Explanation:
- First fall: seen once
- First rebound: rises to 1.98 ➜ seen going up, then again going down
→ Total: 3
*/