-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tortoises race.js
18 lines (17 loc) · 1.02 KB
/
Tortoises race.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//https://www.codewars.com/kata/55e2adece53b4cdcb900006c/train/javascript
//Two tortoises named A and B must run a race. A starts with an average speed of 720 feet per hour. Young B knows she runs faster than A, and furthermore has not finished her cabbage.
//
//When she starts, at last, she can see that A has a 70 feet lead but B's speed is 850 feet per hour. How long will it take B to catch A?
//
//More generally: given two speeds v1 (A's speed, integer > 0) and v2 (B's speed, integer > 0) and a lead g (integer > 0) how long will it take B to catch A?
//
//The result will be an array [hour, min, sec] which is the time needed in hours, minutes and seconds (round down to the nearest second) or a string in some languages.
//
//If v1 >= v2 then return nil, nothing, null, None or {-1, -1, -1} for C++, C, Go, Nim, [] for Kotlin or "-1 -1 -1".
function race(v1, v2, g) {
if (v2 <= v1) {
return null;
}
let time = Math.floor(3600 * g / (v2 - v1));
return [Math.floor(time / 3600), Math.floor(time / 60) % 60, time % 60];
}