---
id: 69c5f3d787b1725d5f00c8ba
title: "Challenge 256: Closest Time Direction"
challengeType: 28
dashedName: challenge-256
---

# --description--

Given two times, determine whether you can get from the first to the second faster by moving forward or backward.

- Times are given in 24-hour format (`"HH:MM"`)
- The clock wraps around (23:59 goes to 00:00 when moving forward, and 00:00 goes to 23:59 when moving backwards)

Return:

- `"forward"` if moving forward is shorter
- `"backward"` if moving backward is shorter
- `"equal"` if both directions take the same amount of time

# --hints--

`getDirection("10:00", "12:00")` should return `"forward"`.

```js
assert.equal(getDirection("10:00", "12:00"), "forward");
```

`getDirection("11:00", "05:00")` should return `"backward"`.

```js
assert.equal(getDirection("11:00", "05:00"), "backward");
```

`getDirection("00:00", "12:00")` should return `"equal"`.

```js
assert.equal(getDirection("00:00", "12:00"), "equal");
```

`getDirection("15:45", "01:10")` should return `"forward"`.

```js
assert.equal(getDirection("15:45", "01:10"), "forward");
```

`getDirection("03:30", "19:50")` should return `"backward"`.

```js
assert.equal(getDirection("03:30", "19:50"), "backward");
```

`getDirection("06:30", "18:30")` should return `"equal"`.

```js
assert.equal(getDirection("06:30", "18:30"), "equal");
```

# --seed--

## --seed-contents--

```js
function getDirection(time1, time2) {

  return time1;
}
```

# --solutions--

```js
function getDirection(time1, time2) {
  function toMinutes(t) {
    const [h, m] = t.split(":").map(Number);
    return h * 60 + m;
  }

  const start = toMinutes(time1);
  const end = toMinutes(time2);

  const forward = (end - start + 1440) % 1440;
  const backward = (start - end + 1440) % 1440;

  if (forward < backward) return "forward";
  if (backward < forward) return "backward";
  return "equal";
}
```
