.some() and .every()
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — Asking a Question About a Whole List
Imagine you are in the middle of a battle with your Codémon team and you need a quick yes or no answer about the whole group: does at least one Codémon still have HP left? Or is every single Codémon able to fight?
The `.some()` method checks if at least one element in the array passes a test. It returns `true` if at least one matches, or `false` if none do:
const team = [
{ name: "Aquachu", hp: 0 },
{ name: "Pyrochu", hp: 25 },
{ name: "Zap", hp: 0 }
];
const canKeepFighting = team.some((cdm) => cdm.hp > 0);
console.log(canKeepFighting);The `.every()` method is stricter: it checks if every single element in the array passes the test. It only returns `true` if all of them pass:
const team = [
{ name: "Aquachu", hp: 0 },
{ name: "Pyrochu", hp: 25 },
{ name: "Zap", hp: 0 }
];
const allAlive = team.every((cdm) => cdm.hp > 0);
console.log(allAlive);Exercise 2 — They Stop Early
Neither `.some()` nor `.every()` checks the entire array if it already knows the final answer: - `.some()` stops at the first `true` it finds (since one match is enough). - `.every()` stops at the first `false` it finds (since one failure ruins the whole test).
Look at what happens when we check if at least one Codémon in the team has reached level 10:
const team = [
{ name: "Aquachu", level: 15 },
{ name: "Pyrochu", level: 2 },
{ name: "Zap", level: 8 }
];
const hasVeteran = team.some((cdm) => {
console.log("Checking " + cdm.name);
return cdm.level >= 10;
});
console.log(hasVeteran);Because `Aquachu` is already level 15, `.some()` immediately returns `true` without ever checking `Pyrochu` or `Zap`. Try changing `15` to `5` to see it check the rest of the team!
Exercise 3 — Quiz: At Least One
What does this code print?
const levels = [4, 8, 12];
console.log(levels.some((l) => l >= 10));truefalse121Hint : One element passing is enough for this method.
Exercise 4 — Quiz: All of Them
What does this code print?
const levels = [4, 8, 12];
console.log(levels.every((l) => l >= 10));falsetrue1[12]Hint : A single failure is enough to settle this one.
Exercise 5 — Anyone Still Fighting?
Print `true` if at least one member still has HP.
Hint : One method asks "is there at least one?", and "still has HP" means strictly more than zero.
Exercise 6 — The Empty Array Surprise
On an empty array, `some` is always `false` and `every` is always `true`. That looks odd, but it follows the definitions: there is no element that passes, and there is no element that fails.
const empty = [];
console.log(empty.some((x) => x > 0));
console.log(empty.every((x) => x > 0));Check the length first
"Every member is ready" being `true` for an empty team is rarely what you want to display. When it matters, test the length before trusting `every`.
Exercise 7 — Straight Into a Condition
Both return a boolean, so they slot into an `if` with no comparison needed. Read out loud, they sound like plain English:
const team = [
{ name: "Aquachu", hp: 0 },
{ name: "Zap", hp: 25 }
];
if (team.every((cdm) => cdm.hp === 0)) {
console.log("All your Codémon fainted!");
} else if (team.some((cdm) => cdm.hp === 0)) {
console.log("Some of your Codémon need healing");
} else {
console.log("Everyone is ready!");
}Exercise 8 — Everyone Ready?
Print `Ready!` only when every member has HP above 0.
Hint : One method requires all of them to pass, and having HP means strictly above zero.
Exercise 9 — some vs every
Match each element with its description.
arr.some(f)arr.every(f)[].some(f)[].every(f)falsetrueExercise 10 — Battle Readiness Check
Before letting the player enter the arena, the game runs a readiness check on the team.
Step 1: print `"Has a fighter: X"` where X says whether at least one member has HP above 0. Step 2: print `"All healthy: X"` where X says whether every member has HP of 30 or more. Step 3: print `"Has a veteran: X"` where X says whether at least one member is level 10 or more. Step 4: if every member is fainted, print `"Go to the Center!"`, otherwise print `"You may enter the arena"`.
Expected output:
Has a fighter: true
All healthy: false
Has a veteran: true
You may enter the arenaconst team = [
{ name: "Aquachu", hp: 40, level: 12 },
{ name: "Pyrochu", hp: 0, level: 8 },
{ name: "Zap", hp: 25, level: 4 }
];
// Write your code here