Shortest Path Problem Leetcode Javascript