A lazy-loading script needs every img on a page, and a link checker needs every a. Both work like getElementsByTagName.
Write byTag(root, tag) that returns the ids of all elements whose tag equals tag, in document order (parent before children, children in order). root itself counts if it matches. Elements are objects with tag, id, and optional children. If root is null or nothing matches, return an empty array.
Input: root = {"tag":"nav","id":"n","children":[{"tag":"a","id":"n1"},{"tag":"a","id":"n2"},{"tag":"button","id":"n3"},{"tag":"a","id":"n4"}]}, tag = "a" Output: ["n1","n2","n4"]
Three links, in the order they appear; the button is skipped.
Input: root = {"tag":"body","id":"root","children":[{"tag":"header","id":"top","children":[{"tag":"h1","id":"title"}]},{"tag":"main","id":"content","children":[{"tag":"p","id":"intro"},{"tag":"ul","id":"list","children":[{"tag":"li","id":"a"},{"tag":"li","id":"b"}]}]}]}, tag = "li" Output: ["a","b"]
Both list items, inside ul#list.
0 to 10^4 elements.Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #701 Find Every Element of One Tag. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.