JavaScript Exercises: Mastering Array filter() Method

JavaScript Exercises: Mastering Array filter() Method

These exercises are designed to help you master the filter() method, which creates a new array with all elements that pass the test implemented by the provided function.

Exercises

1. Filter Even Numbers

Question: Write a function filterEvens that filters out all even numbers from an array.

Required Output for [1, 2, 3, 4]: [1, 3]

2. Remove Falsy Values

Question: Write a function removeFalsy that removes all falsy values from an array.

Required Output for [0, 1, false, 2, '', 3]: [1, 2, 3]

3. Greater Than Five

Question: Write a function greaterThanFive that filters out numbers greater than five.

Required Output for [3, 6, 8, 1]: [6, 8]

4. Filter Specific Value

Question: Write a function filterValue that filters out a specific value from an array.

Required Output for ([1, 2, 3, 2, 4], 2): [1, 3, 4]

5. Strings Longer Than Three Characters

Question: Write a function longStrings that filters out strings longer than three characters.

Required Output for ["yes", "no", "maybe", "ok"]: ["maybe"]

6. Filter Objects by Property

Question: Write a function filterByProperty that filters an array of objects based on a property’s existence.

Required Output for ([{a: 1}, {b: 2}, {a: 3, b: 4}], 'a'): [{a: 1}, {a: 3, b: 4}]

7. Remove Duplicates

Question: Write a function removeDuplicates that removes duplicate elements from an array.

Required Output for [1, 2, 2, 3, 3, 4, 4, 4]: [1, 2, 3, 4]

8. Filter By Length

Question: Write a function filterByLength that filters an array of strings by a specific length.

Required Output for (["hello", "hi", "hey", "hola"], 3): ["hey", "hola"]

9. Numbers Divisible by Three

Question: Write a function divisibleByThree that filters numbers divisible by three.

Required Output for [1, 3, 4, 6, 7, 9]: [3, 6, 9]

10. Filter Valid Emails

Question: Write a function filterEmails that filters valid emails based on the presence of “@”.

Required Output for ["alice@gmail.com", "bob@", "charlie@domain.com", "dave"]: ["alice@gmail.com", "bob@", "charlie@domain.com"]

11. Exclude Specific Words

Question: Write a function excludeWords that filters out specific words from an array.

Required Output for (["apple", "banana", "cherry", "date"], "banana"): ["apple", "cherry", "date"]

12. Filter Adults

Question: Write a function filterAdults that filters persons over the age of 18.

Required Output for ([{name: "Alice", age: 16}, {name: "Bob", age: 20}]): [{name: "Bob", age: 20}]

13. Prime Numbers

Question: Write a function filterPrimes that filters out prime numbers from an array.

Required Output for [2, 3, 4, 5, 6, 7, 8]: [2, 3, 5, 7]

14. Exclude Specific Characters

Question: Write a function excludeChars that filters out specific characters from a string array.

Required Output for (["a", "b", "c", "d"], "c"): ["a", "b", "d"]

15. Filter by Starting Letter

Question: Write a function filterByStartingLetter that filters words starting with a specified letter.

Required Output for (["apple", "banana", "avocado", "grape"], "a"): ["apple", "avocado"]

16. Filter by Score

Question: Write a function filterByScore that filters objects based on a minimum score property.

Required Output for ([{score: 50}, {score: 60}, {score: 70}], 60): [{score: 60}, {score: 70}]

17. Positive Numbers

Question: Write a function filterPositives that filters out all positive numbers from an array.

Required Output for [-1, 2, -3, 4, -5]: [2, 4]

18. Exclude Null Values

Question: Write a function excludeNull that filters out all null values from an array.

Required Output for [null, 1, null, 2, 3, null]: [1, 2, 3]

19. Filter Users by Country

Question: Write a function filterUsersByCountry that filters an array of user objects by a specific country.

Required Output for ([{country: "USA"}, {country: "Canada"}, {country: "USA"}], "USA"): [{country: "USA"}, {country: "USA"}]

20. Filter Decimal Numbers

Question: Write a function filterDecimals that filters out numbers that are not whole numbers.

Required Output for [1.1, 2, 3.5, 4]: [2, 4]

Conclusion

By practicing these exercises, you can gain a solid foundation in JavaScript array filter method.

updatedupdated2024-04-172024-04-17
comments powered by Disqus