Count by Condition
Prompt
We have a nested array that can contain any type of items including other nested arrays. We want to create a JavaScript function that counts the number of items in this array that pass a certain condition. This condition will be given to you as a JavaScript callback function that takes an item as input and returns a boolean value.
Here's an example for clarity: If we have the array [1, 'a', [2, 'b', [3, 'c', [4]]], 'd', [5, 'e']]
and the callback function is (item) => typeof item === 'string'
, the function should return 4, because there are four strings in the array 'a', 'b', 'c', and 'e'
.
Playground
Use recursion to handle the nested arrays. When you come across an item that is also an array, you can call the same function on this nested array.
Use Array.isArray()
to check if an item is an array.
This will help you decide if you need to apply the
callback function or make a recursive call.
Solution
Explanation
Let's say we have an array like this: [1, 'a', [2, 'b', [3, 'c', [4]]], 'd', [5, 'e']]
. If the callback function checks for strings, the expected output would be 4 because there are four strings in the array: 'a', 'b', 'c', and 'e'.
Here's how we can approach this problem:
- Define the Function: First, we define a function called countNestedArray that takes two parameters: arr (the nested array) and callback (the function that defines the condition).
- Input Validation: Before doing any processing, we need to make sure that the inputs are valid. We check if
arr
is an array and ifcallback
is a function. If either of these conditions is not met, we throw an error with an appropriate message. - Initialize a Counter: We initialize a variable
count
to 0. This variable will keep track of the number of items that satisfy the condition. - Iterate Over the Array: We use the
forEach
method to iterate over each item in thearr
array. - Check for Nested Arrays: For each item, we check if it is an array using
Array.isArray(item)
. If it is an array, we recursively call thecountNestedArray
function with the nested array and thecallback
function. The result of this recursive call is added to thecount
variable. - Check the Condition: If the current item is not an array, we call the
callback
function with the item as an argument. If thecallback
function returnstrue
, it means the item satisfies the condition, and we increment thecount
variable. - Return the Result: After iterating over all items in the array, we return the final
count
.
The key aspects of this solution are recursion and callback functions.
Note
The use of the forEach
method to iterate over the array
and the recursive approach to handle nested arrays are
both valid techniques. However, some developers may prefer
using a for...of
loop or a separate recursive helper
function to enhance readability and modularity.
Common Pitfalls
Nested Loops vs Recursion: Some candidates may attempt to solve the problem using nested loops instead of recursion. While this approach can work for simple cases, it may become less efficient and harder to maintain for deeply nested arrays.
Array Methods: Candidates may not be familiar with array methods like Array.isArray()