Question
A child plays with a ball on the nth floor of a big building. The height of this floor is known. (float parameter h in
A child plays with a ball on the nth floor of a big building. The height of this floor is known.
(float parameter "h" in meters. Condition 1) : h > 0)
He lets out the ball. The ball bounces for example to two-thirds of its height.
(float parameter "bounce". Condition 2) : 0 < bounce < 1)
His mother looks out of a window that is 1.5 meters from the ground.
(float parameters "window". Condition 3) : window < h).
How many times will the mother see the ball either falling or bouncing in front of the window?
If all three conditions above are fulfilled, return a positive integer, otherwise return -1.
Note
You will admit that the ball can only be seen if the height of the rebouncing ball is stricty greater than the window parameter.
Example:
h = 3, bounce = 0.66, window = 1.5, result is 3
h = 3, bounce = 1, window = 1.5, result is -1 (Condition 2) not fulfilled).
Im trying to solve this in javascript but im kind of confused about Math.log(). I found a real nice solution in Ruby but when i try to redo in javascript i couldn't figure it out
# Better Solution- this is the Ruby solution | |
def bouncingBall(h, bounce, window) | |
bounce > 0 && bounce < 1 && window < h ? 1 + 2*Math.log(window.to_f/h,bounce).floor : -1 | |
end
|
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started