Question
Im working in elequent javascript on the excersise of element by tag name- chap 14. I am a little confused what is going on by
Im working in elequent javascript on the excersise of 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 explain the check function, with comments in the code.
I posted this question and it was a terrible answer.
(this was the previous post- https://www.chegg.com/homework-help/questions-and-answers/q107024648?utm_source=Adobe&utm_medium=email&utm_campaign=cheggQuestionAnswered&utm_source=Braze&utm_medium=email&utm_campaign=MC_CS_Trn_YourQuestionAnswered_EML_0-P1&utm_content=MC_CS_Trn_YourQuestionAnswered_EML_0-P1)
Can you show a picture of the code, with comments explaining what is being done on each line?
thank you in advance for a clarifying answer.
//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