Uber interview question

Asked to code in JavaScript