Question
Im working elequent javascript element by tag name- chap 14. I am a little confused what is going on by a few lines and not
Im working elequent javascript element by tag name- chap 14. I am a little confused what is going on by a few lines and not sure what is going on. if you could clearly explain the check function, which im getting confused with- I really really would appreciate it. thank you
i would appreciate if you could comment on each line so i could see what is going on. im not sure if my comments is right
//the TagName function takes in a structure and what to check for
function byTagName(node, tagName) {
//change everything to lowercase to compare
tagName = tagName.toLowerCase();
//create a array to contain all the found elements
var tagFound = [];
//the check function will loop through the structure
function check(node) {
//loop through how ever many childNodes there are
for (var i = 0; i < node.childNodes.length; i++) {
//what is going on on this line?????
var childN = node.childNodes[i];
//what is being done on this line????
if (childN.nodeType == document.ELEMENT_NODE) {
//if there is a node with the tagName, push it into the tagFound array
if (childN.nodeName == tagName)
tagfound.push(childN);
//call the function again
check();
}
}
}
return tagFound;
}
Heading with a span element.
A paragraph with one, two
spans.
function byTagName(node, tagName) {
// Your code here.
}
console.log(byTagName(document.body, "h1").length);
// 1
console.log(byTagName(document.body, "span").length);
// 3
let para = document.querySelector("p");
console.log(byTagName(para, "span").length);
// 2
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started