0
0
Javascriptprogramming~20 mins

Find and some methods in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Find and Some Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of find method on array of objects
What is the output of the following code?
Javascript
const users = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 3, name: 'Charlie' }
];

const result = users.find(user => user.id === 2);
console.log(result);
Aundefined
B{ id: 2, name: 'Bob' }
C{ id: 3, name: 'Charlie' }
D[{ id: 2, name: 'Bob' }]
Attempts:
2 left
πŸ’‘ Hint
The find method returns the first element that matches the condition.
❓ Predict Output
intermediate
2:00remaining
Output of some method with condition
What does the following code output?
Javascript
const numbers = [1, 3, 5, 7, 8];
const hasEven = numbers.some(num => num % 2 === 0);
console.log(hasEven);
Atrue
Bfalse
Cundefined
DTypeError
Attempts:
2 left
πŸ’‘ Hint
The some method returns true if any element matches the condition.
❓ Predict Output
advanced
2:00remaining
Find method with no matching element
What is the output of this code?
Javascript
const fruits = ['apple', 'banana', 'cherry'];
const found = fruits.find(fruit => fruit.length > 10);
console.log(found);
Anull
B'' (empty string)
Cundefined
DTypeError
Attempts:
2 left
πŸ’‘ Hint
If find does not find any element, it returns undefined.
❓ Predict Output
advanced
2:00remaining
Some method with all elements failing condition
What will this code output?
Javascript
const ages = [21, 25, 30];
const hasMinor = ages.some(age => age < 18);
console.log(hasMinor);
ASyntaxError
Btrue
Cundefined
Dfalse
Attempts:
2 left
πŸ’‘ Hint
some returns false if no elements satisfy the condition.
🧠 Conceptual
expert
3:00remaining
Understanding behavior of find and some with complex conditions
Given the code below, what is the output of the console.log statement?
Javascript
const data = [
  { id: 1, active: false },
  { id: 2, active: false },
  { id: 3, active: true },
  { id: 4, active: true }
];

const result = data.find(item => item.active && item.id > 3);
const check = data.some(item => item.active && item.id <= 3);
console.log(result, check);
A{ id: 4, active: true } true
B{ id: 3, active: true } false
Cundefined true
Dundefined false
Attempts:
2 left
πŸ’‘ Hint
find returns the first element matching the condition; some returns true if any element matches.