我试图编写一个具有类似文件树结构的树。我试图在'tree.prototype.add'代码中运行一段代码(tree.prototype.traversalBF)。
问题在这里:
Tree.prototype.traversalBF = function(node, pathPart) {
//determines number of children of the given node
console.log('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
var length = node.children.length;
var i = 0;
var found = false;
console.log(node);
//cycles through until there is a match
while( found != true && i < length){
if(node.children[i] == pathPart){
found = true;
//when there is a match it returns the node
return node.children[i];
} else if( i = length) {
var nodeFile = new Node(pathPart);
//adds the file name onto the the node
node.children.push(nodeFile);
//sets the node parent to the currentNode
nodeFile.parent = node;
}
i++;
}
}
Tree.prototype.add = function(path){
var pathSplit = path.split('/');
//gets the length of the path
var pathLength = pathSplit.length;
console.log(pathLength);
//this compares the path to the nodes/directories
let compare = (currentNode, n) => {
//console.log(n);
if(n == pathLength -1){
console.log(pathLength+ "secons");
console.log(currentNode.data)
//create a new node with file name as data
var nodeFile = new Node(pathSplit[n]);
//adds the file name onto the the node
currentNode.children.push(nodeFile);
//sets the node parent to the currentNode
nodeFile.parent = currentNode;
}else{
var newNode = () => this.traversalBF(currentNode, pathSplit[n]);
console.log(newNode);
compare(newNode, n+1);
};
};
compare(this._root, 0);
};
运行else语句,但是如果console.log('!!!! ... !!!')没有记录,则不运行traversalBF。