I have a JavaScript function as:
function nodeExists(text, ancestor, tree, generationCount) {
tree.findByText(text).each(function (index, element) {
var gen = generationCount;
var currNode = element;
while (gen !== 1) { // 1: node itself
currNode = tree.parent(currNode);
gen--;
}
if (tree.text(currNode) === ancestor)
return currNode; // Even if condition is met, control continues looping
})
return null;
//return ($.inArray(ancestor, gArr) !== -1) ? true : false;
}
While debugging function not exiting from a loop even if tree.text(currNode) === ancestor is truthy. Is Jquery .each causing it. Please help me.
while (gen !== 1)but instead,while (gen > 1), just in case something goes wrong.falsewill stop the iteration witheach, usebreakorreturn falsesoelement == currNode