Node.JS Solution

Code is not optimized but should be easier to visualize.