Here we go again with the beginner guy. I'm working on this exercise from a book called "Eloquent JavaScript". The goal is to create a function similar to "getElementByTagName". However, the first function below is not returning anything and the second one is struggling to pass through the "if" statement after the "while" loop.
Cheers,
function findTags(node, tagName) {
let tagArray = [];
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 == tagName.toUpperCase()) {
tagArray.push(currentNode);
}
let child = findTags(currentNode, tagName )
if(child) {
tagArray.concat(child);
}
}
}
return tagArray;
}
function findTags(node, tagName) {
let tagArray = [];
let currentNode = node.firstElementChild;
while(currentNode != null) {
if(currentNode.tagName == tagName.toUpperCase()) {
tagArray.push(currentNode);
}
let descendant = findTags(currentNode, tagName );
if(descendant) {
tagArray.concat(descendant);
}
let currentNode = currentNode.nextElementSibling;
}
return tagArray;
}