EmptyCup interview question

solve this algorithm in javascript