It,s the noob guy again. I doing this exercise from a book I am reading (Eloquent JavaScript). it,s suppose to work like "getElementByTagName". The first function below does not return anything. The second one can't get pass through the "if" statement after "while".
Thanks,
function tagSearch (node, name) {
let tagArrays = [];
let childrenNode = node.childNodes;
if(childrenNode.length != 0) {
for(let i = 0; i < childrenNode.length; i++) {
let currentNode = childrenNode[i];
if(currentNode == Node.ELEMENT_NODE && currentNode.nodeName == name.toUpperCase()) {
tagArrays.push(currentNode);
}
let child = tagSearch(currentNode, name )
if(child) {
tagArrays.concat(child);
}
}
}
return tagArrays;
}
function tagSearch (node, name) {
let tagArrays = [];
let currentNode = node.firstElementChild;
while(currentNode != null) {
if(currentNode.tagName == name.toUpperCase()) {
tagArrays.push(currentNode);
}
let descendant = tagSearch(currentNode, name );
if(descendant) {
tagArrays.concat(descendant);
}
let currentNode = currentNode.nextElementSibling;
}
return tagArrays;
}