Mode entretien. Minuteur de 10 min, sans indices ni autocomplétion
Nearest lift
MoyenBasics10 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.
Exemples
chooseLift(5, 1, 2)Renvoie 'B'
chooseLift(0, 4, 2)Renvoie 'A'
chooseLift(3, 3, 1)Renvoie 'A'
Posé chez Goomo · Source
Chargement de l'éditeur...
- Entrée
chooseLift(5, 1, 2)Attendu'B' - Entrée
chooseLift(2, 7, 2)Attendu'A' - Entrée
chooseLift(3, 3, 1)Attendu'A'
Les tests cachés se lancent à la soumission