Skip to the solution
EasyDSA6 min read

Implement Array.prototype.some() and every() From Scratch

687 views

The problem

Problem, as an interviewer would state it: "Implement Array.prototype.mySome and myEvery from scratch — matching real native behavior, including that both genuinely SHORT-CIRCUIT rather than checking every element."

Examples:

Code
[1,2,3,4,5].mySome(n => n === 3); // true, and STOPS checking after finding it

Clarifying questions expected:

  • Do both methods need to genuinely stop early once the answer is already determined, or is checking every element acceptable?
  • What is the real, correct result for an empty array — for some(), and separately for every()?
  • Do these two methods need to be implemented as genuinely separate functions, or could one be derived from the other?

Code / implementation expected: Yes — real, direct proof of correct output matching native, PLUS real, direct proof of genuine short-circuiting via a logged list of exactly which elements were checked before stopping.

  • polyfill
  • array

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read · 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, direct proof: some()/every() genuinely short-circuit, confirmed via a real logged list of exactly which elements were checked before stopping

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 34 of 154 in JavaScript coding

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextEasy

Polyfill Array.prototype.filter from scratch

Continue