Code Lab

Modo entrevista. Temporizador de 10 min, sin pistas y sin autocompletado

Nearest lift

MediaBasics10 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.

Ejemplos

chooseLift(5, 1, 2)

Devuelve 'B'

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

Devuelve 'A'

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

Devuelve 'A'

Preguntada en Goomo · Fuente

Cargando el editor…
  1. EntradachooseLift(5, 1, 2)
    Esperado'B'
  2. EntradachooseLift(2, 7, 2)
    Esperado'A'
  3. EntradachooseLift(3, 3, 1)
    Esperado'A'

Las pruebas ocultas se ejecutan al enviar