Leetcode Breadth First Search Algorithm Javascript For Loop