Problem, as an interviewer would state it:
"Implement your own version of document.getElementById(id), walking the DOM tree by hand rather than using the real built-in method. Show me it returns the exact same node the real one would."
Examples:
findById(document.body, "submit-button") // the actual matching element, or null
Clarifying questions expected:
- Should the search start from
document/document.body, or should it accept any root node to search from? - What should it return if no element has that id —
null, orundefined? - Should it stop at the FIRST match if (invalidly) multiple elements share an id, matching real browser behavior?
Code / implementation expected: Yes — real, direct proof against a real jsdom document that the hand-written traversal returns the identical node reference the browser's own getElementById would.