breadth-first search algorithm in javascript