feat(curriculum): Add interactive examples to Every and some methods lesson (#63392)

This commit is contained in:
Giftea ☕
2025-10-31 21:24:59 +01:00
committed by GitHub
parent b07b75ba0e
commit 7675b19f01

View File

@@ -5,7 +5,7 @@ challengeType: 19
dashedName: how-do-the-every-and-some-methods-work
---
# --description--
# --interactive--
When you're working with arrays in JavaScript, there are often times when you want to check if all elements in an array meet a certain condition, or if at least one element meets a condition.
@@ -17,6 +17,8 @@ The `every()` method returns `true` if the provided function returns `true` for
Here's an example to illustrate how `every()` works:
:::interactive_editor
```js
const numbers = [2, 4, 6, 8, 10];
const hasAllEvenNumbers = numbers.every((num) => num % 2 === 0);
@@ -24,6 +26,8 @@ const hasAllEvenNumbers = numbers.every((num) => num % 2 === 0);
console.log(hasAllEvenNumbers); // true
```
:::
In this example, we're checking if all numbers in the array are even. The function we provide to `every()` checks if each number is divisible by `2` with no remainder. Since all numbers in our array are indeed even, `hasAllEvenNumbers` will be true.
Now, let's look at the `some()` method.
@@ -32,6 +36,8 @@ While `every()` checks if all elements pass a test, `some()` checks if at least
Here's an example of how `some()` works:
:::interactive_editor
```js
const numbers = [1, 3, 5, 7, 8, 9];
const hasSomeEvenNumbers = numbers.some((num) => num % 2 === 0);
@@ -39,6 +45,8 @@ const hasSomeEvenNumbers = numbers.some((num) => num % 2 === 0);
console.log(hasSomeEvenNumbers); // true
```
:::
In this example, we're checking whether any number in the array is even. The function we pass to `some()` is the same as before. Even though most numbers in our array are odd, `hasEven` will be `true` because there's at least one even number (`8`) in the array.
Both `every()` and `some()` are very useful when you need to validate data or check for certain conditions in your arrays. They can often replace more verbose loops and conditional statements, making your code cleaner and more expressive.