Code Lab

Modo entrevista. Cronômetro de 10 min, sem dicas e sem autocompletar

Nearest lift

MédiaBasics10 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.

Exemplos

chooseLift(5, 1, 2)

Retorna 'B'

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

Retorna 'A'

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

Retorna 'A'

Cobrada em Goomo · Fonte

Carregando o editor...
  1. EntradachooseLift(5, 1, 2)
    Esperado'B'
  2. EntradachooseLift(2, 7, 2)
    Esperado'A'
  3. EntradachooseLift(3, 3, 1)
    Esperado'A'

Os testes ocultos rodam no envio