Javascript Fundamentals · Array Methods

Test for any or all

some and every

Test for any or all

You have exam scores and a pass mark of 60, and you need two quick verdicts: whether every student passed, and whether anyone failed. Looping over the whole list to answer either question is work you do not need to finish.

Two methods split the job. some answers "does at least one element pass?"; every answers "do all of them pass?". Both take a predicate and return a boolean, and both stop as soon as the answer is certain.

const scores = [90, 55, 72];

scores.every((score) => score >= 60); // false — 55 fails
scores.some((score) => score < 60); // true — at least one fails

some returns true on the first truthy result; every returns false on the first falsy one. That early exit is the point: neither method needs to visit the rest of the array once the verdict is decided.

The pitfall: the empty array. some returns false — there is no witness — while every returns true — there is no counterexample. The two are exact opposites, so pick the one that reads as the question you are actually asking.

Export allPassing and anyFailing from index.js. Given a list of scores, allPassing returns true only when every score is at least 60, and anyFailing returns true when at least one score is below 60.

Challenge

You have a list of exam scores and a pass mark of 60, and you need two verdicts: did everyone pass, and did anyone fail? Checking each score by hand invites off-by-one logic. Export allPassing and anyFailing from index.js to answer both.

Each takes an array of numeric scores. allPassing returns true only when every score is at least 60, otherwise false; anyFailing returns true when at least one score is below 60, otherwise false.

index.js
export function allPassing(_scores) {
  return true;
}

export function anyFailing(_scores) {
  return false;
}

export function render(mount) {
  const scores = [90, 55, 72];

  mount.textContent = `All passing: ${allPassing(scores)} · Any failing: ${anyFailing(scores)}`;
}

No test run yet

The Test is hidden. Run it to check the Challenge — you only see the result, never the test source.