Filter Out Strings from an Array
Imagine you are working on a data processing application that receives mixed data types in the form of arrays. Some of the data consists of non-negative integers, while other parts of the data are strings. For further analysis, you need to filter out all the strings from these arrays and only keep the non-negative integers.
Instructions:
-
Write a function named
filterArray
that takes one parameter:mixedArray
: An array containing non-negative integers and strings.
-
The function should return a new array that contains only the non-negative integers from the original
mixedArray
, with all the strings removed.
Note: Only use the map
, filter
, or reduce
methods, or a combination of these methods, to solve the problem. Do not use loops to iterate or perform the operation.
Example 1
Input: filterArray([1, "a", "b", 0, 15]);
Output: [1, 0, 15]
Example 2
Input: filterArray(["apple", 3, "banana", 4, 5, "orange"]);
Output: [3, 4, 5]
Example 3
Input: filterArray(["hello", "world"]);
Output: []
Are you stuck on this problem?
Get detailed, step-by-step solution and improve your JavaScript skills.
- Detailed Explanation: Each solution comes with a comprehensive explanation, helping you understand the logic and concepts thoroughly.
- Learn to Write Better Code: Even if you have written the code, discover ways to improve it and adopt best practices.
- Save Time: Don’t waste hours struggling—get the solution and learn efficiently.
- Lifetime Access: Get access to lifetime solutions for all problems available
" I struggled with this problem for hours, but the solution provided here was clear and easy to follow. It helped me understand where I went wrong and improved my coding skills significantly." - Jane D.
If you're not satisfied, I offer a 7-day money-back guarantee.