Nearest lift
MediumBasics10 min
Write a function chooseLift(liftA, liftB, callFloor) that returns which of two lifts goes to a person: the string 'A' or 'B'. A building has two lifts, A and B. The person is on floor callFloor and presses the "up" button.
- All three parameters are floor numbers: whole numbers from 0.
liftAandliftBare the floors where the lifts stand now. - The distance from a lift to the person is the absolute value of the difference of the floors:
Math.abs(liftA - callFloor). - The lift with the smaller distance goes.
- If the distances are equal and the lifts are on different floors, the lift below
callFloorgoes: it moves up anyway, the same way the person wants to go. - If both lifts are on the same floor,
'A'goes. - Return the name of the lift, not its floor.
An if statement with comparison operators is enough.
Examples
chooseLift(5, 1, 2)Returns 'B'
chooseLift(0, 4, 2)Returns 'A'
chooseLift(3, 3, 1)Returns 'A'
Asked at Goomo · Source
Loading the editor...
- Input
chooseLift(5, 1, 2)Expected'B' - Input
chooseLift(2, 7, 2)Expected'A' - Input
chooseLift(3, 3, 1)Expected'A'
Hidden tests run on Submit