0
0
Javascriptprogramming~20 mins

Filter method in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Filter Method Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this filter method?
Look at this JavaScript code. What will it print to the console?
Javascript
const numbers = [1, 2, 3, 4, 5];
const result = numbers.filter(num => num > 3);
console.log(result);
A[4, 5]
B[1, 2, 3]
C[]
D[3, 4, 5]
Attempts:
2 left
πŸ’‘ Hint
Filter keeps only items where the condition is true.
❓ Predict Output
intermediate
2:00remaining
What does this filter return?
What will be the output of this code snippet?
Javascript
const words = ['apple', 'banana', 'cherry', 'date'];
const filtered = words.filter(word => word.length === 5);
console.log(filtered);
A['apple', 'banana']
B['apple']
C['apple', 'cherry']
D['banana', 'date']
Attempts:
2 left
πŸ’‘ Hint
Check the length of each word carefully.
❓ Predict Output
advanced
2:00remaining
What is the output of this filter with objects?
What will this code print to the console?
Javascript
const people = [
  {name: 'Alice', age: 25},
  {name: 'Bob', age: 17},
  {name: 'Carol', age: 19}
];
const adults = people.filter(person => person.age >= 18);
console.log(adults);
A[{name: 'Bob', age: 17}]
B[{name: 'Alice', age: 25}]
C[{name: 'Alice', age: 25}, {name: 'Carol', age: 19}]
D[]
Attempts:
2 left
πŸ’‘ Hint
Filter keeps people who are 18 or older.
❓ Predict Output
advanced
2:00remaining
What happens with this filter callback?
What will this code output?
Javascript
const arr = [0, 1, 2, 3];
const filtered = arr.filter(x => x);
console.log(filtered);
A[1, 2, 3]
B[0, 1, 2, 3]
C[]
D[0]
Attempts:
2 left
πŸ’‘ Hint
Filter keeps items where the callback returns a truthy value.
🧠 Conceptual
expert
2:00remaining
Which option causes a runtime error?
Which of these filter uses will cause a runtime error when run?
A['a', 'b', 'c'].filter((x, i) => i > 1)
B[1, 2, 3].filter(x => x > 1)
C[true, false].filter(x => x === true)
D[null, undefined].filter(x => x.length > 0)
Attempts:
2 left
πŸ’‘ Hint
Check if the callback tries to access a property on null or undefined.