0
0
Javascriptprogramming~10 mins

Filter method in Javascript - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to filter out numbers less than 5 from the array.

Javascript
const numbers = [1, 4, 6, 8];
const filtered = numbers.filter(num => num [1] 5);
console.log(filtered);
Drag options to blanks, or click blank then click option'
A===
B>
C<
D!==
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using '<' will keep numbers less than 5, which is the opposite.
Using '===' or '!==' compares equality, not greater or less.
2fill in blank
medium

Complete the code to filter only even numbers from the array.

Javascript
const nums = [1, 2, 3, 4, 5];
const evens = nums.filter(n => n [1] 2 === 0);
console.log(evens);
Drag options to blanks, or click blank then click option'
A%
B*
C/
D+
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using '/' divides but does not check remainder.
Using '*' or '+' does not help check even numbers.
3fill in blank
hard

Fix the error in the filter callback to keep strings longer than 3 characters.

Javascript
const words = ['cat', 'lion', 'dog', 'elephant'];
const longWords = words.filter(word => word.length [1] 3);
console.log(longWords);
Drag options to blanks, or click blank then click option'
A>
B<
C===
D!==
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using '<' keeps shorter words, which is wrong here.
Using '===' or '!==' checks equality, not length comparison.
4fill in blank
hard

Fill both blanks to filter numbers divisible by 3 from the array.

Javascript
const arr = [3, 4, 6, 7, 9];
const divisibleByThree = arr.filter(num => num [1] 3 [2] 0);
console.log(divisibleByThree);
Drag options to blanks, or click blank then click option'
A%
B===
C!==
D>
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using '!== 0' keeps numbers not divisible by 3.
Using '>' does not check divisibility.
5fill in blank
hard

Fill all three blanks to filter words starting with letter 'a' (case insensitive).

Javascript
const words = ['Apple', 'banana', 'Avocado', 'cherry'];
const aWords = words.filter(word => word.[1](0).[2]() [3] 'a');
console.log(aWords);
Drag options to blanks, or click blank then click option'
AtoLowerCase
BtoUpperCase
C===
DcharAt
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'toUpperCase' on the whole word instead of the first letter.
Not converting case leads to missing words starting with uppercase 'A'.