Problem, as an interviewer would state it:
"Write a function that computes an element's ABSOLUTE position on the page — relative to the whole document, not just its immediate parent — by walking up its offsetParent chain."
Examples:
getAbsoluteOffset(deeplyNestedElement); // { top: 340, left: 120 }, relative to the document
Clarifying questions expected:
- Is
element.offsetTop/offsetLeftrelative to the element's immediateoffsetParent, or already relative to the document? - Does the chain terminate at
document.body, or couldoffsetParentbenullearlier for a positioned or hidden element? - Would
getBoundingClientRect()be a simpler, real alternative — and if so, why walk the offsetParent chain manually at all?
Code / implementation expected: Yes — real, direct proof of the summing algorithm against a deliberately multi-level nested chain, producing the exact correct combined coordinate.