Code Lab

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. liftA and liftB are 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 callFloor goes: 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'

A is 3 floors away, B is 1 floor away.
chooseLift(0, 4, 2)

Returns 'A'

Both are 2 floors away. A is below the person.
chooseLift(3, 3, 1)

Returns 'A'

Asked at Goomo · Source

Loading the editor...
  1. InputchooseLift(5, 1, 2)
    Expected'B'
  2. InputchooseLift(2, 7, 2)
    Expected'A'
  3. InputchooseLift(3, 3, 1)
    Expected'A'

Hidden tests run on Submit.