1

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;
}

2 Answers 2

1

You missed something in your condition. Here you're comparing node with expected node type:

currentNode == Node.ELEMENT_NODE

You need to check node's type, not node itself:

currentNode.nodeType == Node.ELEMENT_NODE

Also, this code is not doing anything:

tagArrays.concat(child);

concat() function returns new array, not mutating old one. So, to add items to tagArrays you should do:

tagArrays = tagArrays.concat(child);

or 

tagArrays.push(...child);

or

tagArrays.splice(tagArrays.length - 1, 0, child);
2
  • Thanks. That was embarrassing. But thanks again, I appreciate it. Commented Dec 6, 2019 at 4:41
  • if this answer is correct for you, you can mark it as right one with check icon for others to see
    – Andres
    Commented Dec 6, 2019 at 13:52
1

You're currently comparing currentNode (a Node) to Node.ELEMENT_NODE (a number), which will always evaluate to false. You need to add the nodeType property to currentNode in your first if statement inside the for loop:

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.nodeType == Node.ELEMENT_NODE && currentNode.nodeName == name.toUpperCase()) {
                tagArrays.push(currentNode);
            }
            let child = tagSearch(currentNode, name )
            if(child) {
                tagArrays.concat(child);
            }
        }    
    }
    return tagArrays;
}
1
  • Thanks. That was embarrassing. But thanks again, I appreciate it. Commented Dec 6, 2019 at 4:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.