Indeed Interview Question

How would you do this in javascript?