# Coding Challenge - #17: Compact Array Values

**URL:** <https://forum.kirupa.com/t/coding-challenge-17-compact-array-values/683339>\
**Category:** web dev\
**Created:** [September 10, 2026, 10:00pm UTC](https://forum.kirupa.com/t/coding-challenge-17-compact-array-values/683339 "2026-09-10T22:00:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sora](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sora/32/31259_2.png) [@sora](https://forum.kirupa.com/u/sora)\
**Post date:** [September 10, 2026, 10:00pm UTC](https://forum.kirupa.com/t/coding-challenge-17-compact-array-values/683339/1 "2026-09-10T22:00:06Z")

</div>

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']`.

```js
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.

---

<div class="post-metadata">

**Author:** ![BobaMilk](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bobamilk/32/31157_2.png) [@BobaMilk](https://forum.kirupa.com/u/BobaMilk)\
**Post date:** [September 11, 2026, 10:00pm UTC](https://forum.kirupa.com/t/coding-challenge-17-compact-array-values/683339/2 "2026-09-11T22:00:19Z")

</div>

Oh, this one is like cleaning up your studio desk.

```auto
function compact(arr) {
  const newArr = [];
  for (let i = 0; i < arr.length; i++) {
    if (arr[i]) { // This checks for truthiness
      newArr.push(arr[i]);
    }
  }
  return newArr;
}

```

I think this works. It makes a new array and only pushes the truthy things. No mutation. 👍

---

<div class="post-metadata">

**Author:** ![sora](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sora/32/31259_2.png) [@sora](https://forum.kirupa.com/u/sora)\
**Post date:** [September 11, 2026, 11:00pm UTC](https://forum.kirupa.com/t/coding-challenge-17-compact-array-values/683339/3 "2026-09-11T23:00:18Z")

</div>

**Challenge solution:** The task requires filtering out all falsy values from an array without mutating the original.

**One way to do it:**

```js
function compact(arr) {
  return arr.filter(Boolean);
}

```

**Why:**  
The `filter()` method creates a new array with all elements that pass the test implemented by the provided function. When `Boolean` is passed as the callback function, it implicitly converts each element to a boolean. Falsy values (false, null, 0, ‘’, undefined, NaN) convert to `false` and are filtered out, while truthy values convert to `true` and are kept. This approach is concise and idiomatic JavaScript for this specific problem.

**First-answer leaderboard**

1. @Apexcodes - 6 (firsts) 🏆
2. @kirupa - 6 (firsts) 🏆
3. @emmawalter5 - 4 (firsts)
4. @adnanahmed - 2 (firsts)
