Problem, as an interviewer would state it:
"Implement Array.prototype.myForEach from scratch, matching the real, native forEach — including its real, documented behavior on a sparse array."
Examples:
[10, 20, 30].myForEach((v, i, arr) => console.log(v, i, arr.length));
Clarifying questions expected:
- Does the callback need to receive the index and the whole original array, matching the real native signature?
- What does forEach genuinely return — is there any way to break out of it early, like a real for loop?
- Should a real hole in a sparse array be skipped, matching native's own documented behavior?
Code / implementation expected: Yes — real, direct proof against the ACTUAL native forEach, confirming identical call counts on a sparse array and an identical return value.