JavaScript fun: The tortoise race

Source: Internet
Author: User
Two turtles, A and B, are racing. A is moving at an hourly rate of 720 feet. Young B knows that A is slower than it, so he is still eating vegetables in A hurry. Two turtles, A and B, are racing.

A is moving at an hourly rate of 720 feet.

Young B knows that A is slower than it, so he is still eating vegetables in A hurry.

When B starts to run, it finds that A is 70 feet ahead, but B's speed is 850 feet per hour, so it will certainly catch up.

How long does B catch up with?

More common cases: Given two speeds: v1 (A's speed,> an integer of 0), v2 (B's speed,> an integer of 0 ), there is also A leading GAP g (g> 0). How long does B have to catch up with?

The result should be an array. [h, mn, s], h, mn, s indicates the hour, minute, and second.

If an exception occurs, such as v1> = v2, B will never catch up with A, then null is returned directly.

For example:

race(720, 850, 70) // => [0, 32, 18]  race(80, 91, 37)   // => [3, 21, 49]

There are two key points for this question:

First, you have to figure out the relationship. In the process of B catching up with A, A has never been idle and has not stopped!

So if we want B to catch up with A, we must satisfy this equation:

V1 * time + g = v2 * time

In this way, time is easy, but the most important thing is how to split the time into minutes and seconds.

My approach is to calculate the clock first, and then the minute based on the remainder, and then the second based on the remainder.

function race(v1, v2, g) {      var h = -1;      var mn = -1;      var s = -1;      var remainder;      var speedGap = v2 - v1;      if(speedGap > 0){          remainder = g % speedGap;          h = parseInt(g / speedGap);          mn = parseInt(60 * remainder / speedGap);          remainder = remainder * 60 % speedGap;          s = parseInt(remainder * 60 / speedGap);          return [h, mn, s];      }      return null;  }

The above is JavaScript fun: the content of the tortoise race. For more information, please follow the PHP Chinese Network (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.