Question
This is assignment element by tag name. https://eloquentjavascript.net/14_dom.html It is giving me a issue running this code. can you please tell me what i did
This is assignment element by tag name. https://eloquentjavascript.net/14_dom.html
It is giving me a issue running this code. can you please tell me what i did wrong? thank you
This is my code
//the TagName function searches the node for any elements of the specified tagName
function byTagName(node, tagName) {
//change everything to lowercase to compare
tagName = tagName.toLowerCase();
//create a array to store all the found element nodes
var tagFound = [];
//start the recursive search of the given node
check(node);
//the check function will check for element nodes with the specified tagName
function check(node) {
//loop through the childNodes of the given node
for (var i = 0; i < node.childNodes.length; i++) {
//assign the childNode a varible
var child = node.childNodes[i];
//checks if it the childNode is an element node
if (child.nodeType == document.ELEMENT_NODE) {
//check if the nodeName is the same as the tagName
if (child.nodeName == tagName)
//if nodeName matches the tag name, childNode is added to the tagFound array
tagFound.push(child);
//call the function again with the childNode
check(child);
}
}
}
return tagFound;
}
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
Heading with a span element.
A paragraph with one, two
spans.
the body they gave me, as you see in the instructions
Thank you in advance
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