I have some variables that are not included in the function but just outside of where they are created. I know I can pass them as parameters however I am looking for a more elegant method.
Code:
Tree.prototype.add = function(path){
var pathSplit = path.split('/');
//gets the length of the path
const pathLength = pathSplit.length;
//this compares the path to the nodes/directories
let compare = (currentNode, n) => {
console.log(n);
if(n == pathLength -1){
console.log(pathLength);
//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{
//console.log('THIS IS RUN');
var newNode = () => this.traversalBF(currentNode, pathSplit[n]);
//console.log(newNode);
compare(newNode, n++);
};
};
compare(this._root, 0);
};
the variable PathLength
is considered to be 0
inside the compare function. however it should be 3
when it is called:
tree.add('one/two/three');