Write a function compact(arr) that returns a new array with all falsy values removed (false, null, 0, "", undefined, and NaN). For example, compact([0, 1, false, 2, '', 3, 'a', NaN]) should return [1, 2, 3, 'a'].
function compact(arr) {
// Your code here
}
Rules:
- Do not mutate the original array
- Must filter out all six JavaScript falsy values accurately
- Return a new array with the remaining elements in their original order
Post your solution as a reply. Answer goes up in about a day.