-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclosest_elevator.js
21 lines (19 loc) · 954 Bytes
/
closest_elevator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Description:
// Given 2 elevators (named "left" and "right") in a building with 3 floors (numbered 0 to 2), write a function accepting 3 arguments (in order):
// left - The current floor of the left elevator
// right - The current floor of the right elevator
// call - The floor that called an elevator
// It should return the name of the elevator closest to the called floor ("left"/"right").
// In the case where both elevators are equally distant from the called floor, choose the elevator to the right.
// You can assume that the inputs will always be valid integers between 0-2.
// Examples:
// left right call result
// 0 1 0 "left"
// 0 1 1 "right"
// 0 1 2 "right"
// 0 0 0 "right"
// 0 2 1 "right"
// SOLUTION:
function elevator(left, right, call) {
return (call === left && call !== right || left > call && left < right || left > right && left < call) ? 'left' : 'right';
}