Exercise 8: Remove Falsy Values from Array
Problem Statement
Create a function `compact(arr)` that removes falsy values (false, null, 0, "", undefined, NaN) from array.
Example: compact([0, 1, false, 2, "", 3]) → [1, 2, 3]
Sample Output:
compact([0, 1, false, 2, "", 3]) => [1, 2, 3] compact([null, undefined, 0, "", NaN, 1]) => [1]
Solution
const compact = (arr) => arr.filter(Boolean);Explanation
Overall Goal:
- Array se falsy values remove karna.
Real world:
- Data cleaning: remove empty/invalid values.
- Form data: filter empty fields.