Implement getElementsByTagName
Implement getElementsByTagName(element, tagName) in dom-query.js: a pure function that returns an array of the element's descendants with that tag name — like the DOM's own method, but without calling it, and without querySelectorAll, which would make the question trivial.
- Only descendants are searched, never the element itself.
- Tag names are case-insensitive:
"div"and"DIV"find the same elements. - Results come back in document order.
- Text and comment nodes are never results and must not break the walk.
The page has a nested fixture: type a tag name and the matches are highlighted, counted, and checked against the browser's own answer.
Hints
Hint 1
node.children is the element-only view of a node's contents; childNodes includes text and comments. Pick the one that makes the type check disappear.
Hint 2
An HTML element's tagName is upper case, so upper-case the argument once and compare with ===.
Hint 3
Recurse into each child after testing it and before moving to its sibling: that is pre-order depth-first, which is document order.
Asked at
Sign in to use the assistant — ask what the problem means, get a hint when you're stuck, or have your current work reviewed. Conversations save to your account.