---
id: 6a2037a68a0bc2aef0006005
title: "Challenge 343: Elevator Stops"
challengeType: 28
dashedName: challenge-343
---

# --description--

Given a number for the current floor of an elevator and an array of requested floors, return an array of the order the elevator should visit them to minimize number of floors traveled.

- If tied, go up first
- Floors with a request must be visited when the elevator first passes them

# --hints--

`elevatorStops(5, [2, 8, 3, 9])` should return `[3, 2, 8, 9]`.

```js
assert.deepEqual(elevatorStops(5, [2, 8, 3, 9]), [3, 2, 8, 9]);
```

`elevatorStops(6, [2, 10, 8, 3, 1, 9])` should return `[8, 9, 10, 3, 2, 1]`.

```js
assert.deepEqual(elevatorStops(6, [2, 10, 8, 3, 1, 9]), [8, 9, 10, 3, 2, 1]);
```

`elevatorStops(1, [4, 8, 3, 6, 9])` should return `[3, 4, 6, 8, 9]`.

```js
assert.deepEqual(elevatorStops(1, [4, 8, 3, 6, 9]), [3, 4, 6, 8, 9]);
```

`elevatorStops(12, [6, 10, 7, 3, 1, 4])` should return `[10, 7, 6, 4, 3, 1]`.

```js
assert.deepEqual(elevatorStops(12, [6, 10, 7, 3, 1, 4]), [10, 7, 6, 4, 3, 1]);
```

`elevatorStops(11, [2, 8, 23, 5, 12, 10, 6, 9, 19])` should return `[10, 9, 8, 6, 5, 2, 12, 19, 23]`.

```js
assert.deepEqual(elevatorStops(11, [2, 8, 23, 5, 12, 10, 6, 9, 19]), [10, 9, 8, 6, 5, 2, 12, 19, 23]);
```

# --seed--

## --seed-contents--

```js
function elevatorStops(currentFloor, stops) {

  return currentFloor;
}
```

# --solutions--

```js
function elevatorStops(currentFloor, stops) {
  stops = stops.sort((a, b) => a - b);
  const above = stops.filter(s => s > currentFloor);
  const below = stops.filter(s => s < currentFloor).reverse();
  const upDistance = above.length ? above[above.length - 1] - currentFloor : Infinity;
  const downDistance = below.length ? currentFloor - below[below.length - 1] : Infinity;
  return downDistance < upDistance ? [...below, ...above] : [...above, ...below];
}
```
