Я не понимаю, почему не срабатывает оператор return на 78 строке
class Graph {
constructor() {
this.nodes = [];
this.edges = [];
}
addNode(value) {
this.nodes.push({
value: value,
visit: false,
lines: []
});
}
find(value) {
return this.nodes.find(node => {
return node.value === value;
});
}
addLine(valueA, valueB) {
var nodeA = this.find(valueA);
var nodeB = this.find(valueB);
if (!nodeA) {
this.addNode(valueA);
nodeA = this.find(valueA);
}
if (!nodeB) {
this.addNode(valueB);
nodeB = this.find(valueB);
}
nodeA.lines.push(nodeB);
nodeB.lines.push(nodeA);
}
setVisit(node, bool) {
node.visit = bool;
}
getVisit(node) {
return node.visit;
}
}
function clone(array) {
var copy = [];
array.forEach(element => {
copy.push(element);
});
return copy;
}
function breadthFirstSearch(start) {
var chains = {},
queueValuesToExplore = [start.value];
width.setVisit(start, true);
var step = [];
step.push(start.value);
chains[start.value] = step;
while (queueValuesToExplore.length > 0) {
let current = queueValuesToExplore.shift();
let neighbors = width.find(current).lines;
let step = chains[current];
delete chains[current];
neighbors.forEach(childItem => {
if (childItem === finish) {
let cloneStep = clone(step);
cloneStep.push(childItem.value);
chains[childItem.value] = cloneStep;
console.log('RETURN ', chains[childItem.value]);
return chains[childItem.value];
} else {
if (!width.getVisit(childItem)) {
let cloneStep = clone(step);
width.setVisit(childItem, true);
queueValuesToExplore.push(childItem.value);
cloneStep.push(childItem.value);
chains[childItem.value] = cloneStep;
}
}
});
}
return null;
}
var width = new Graph();
width.addLine("a", "b");
width.addLine("a", "c");
width.addLine("b", "e");
var start = width.find("a"),
finish = width.find("e");
var result = breadthFirstSearch(start);
console.log(result);