Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 72473

Do I need to turn an HTMLCollection or a Nodelist to create an Array of nodes?

$
0
0

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;

}


Viewing all articles
Browse latest Browse all 72473

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>