Solution for Water Bucket problem in JavaScript