Indeed interview question

How would you do this in javascript?