EmptyCup Interview Question

solve this algorithm in javascript