Leetcode Breadth First Search Algorithm Javascript Foreach