# Drop it - why can't I test for elements that pass function and push them to new array

**URL:** <https://forum.freecodecamp.org/t/drop-it-why-cant-i-test-for-elements-that-pass-function-and-push-them-to-new-array/287583>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [June 20, 2019, 11:11am UTC](https://forum.freecodecamp.org/t/drop-it-why-cant-i-test-for-elements-that-pass-function-and-push-them-to-new-array/287583 "2019-06-20T11:11:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![colfah](https://avatars.discourse-cdn.com/v4/letter/c/a9adbd/32.png) [@colfah](https://forum.freecodecamp.org/u/colfah)\
**Post date:** [June 20, 2019, 11:11am UTC](https://forum.freecodecamp.org/t/drop-it-why-cant-i-test-for-elements-that-pass-function-and-push-them-to-new-array/287583/1 "2019-06-20T11:11:57Z")

</div>

Hi

I can follow the solution to this problem.

However, when I started coding by myself, I tried to push the elements that passed the function to a new array.

I don’t understand why I can’t do this.

**Your code so far**

```js

function dropElements(arr, func) {
  
  
  // for loop with if statement to splice

let answer = [];

  for(let i = 0; i < arr.length; i++){
    if (func(arr[i])){
        answer.push[i];
    }
  }
  return answer;
}

console.log(dropElements([1, 2, 3], function(n) {return n < 3; }));

```

**Your browser information:**

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36`.

**Link to the challenge:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/drop-it)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [June 20, 2019, 11:55am UTC](https://forum.freecodecamp.org/t/drop-it-why-cant-i-test-for-elements-that-pass-function-and-push-them-to-new-array/287583/2 "2019-06-20T11:55:00Z")

</div>

That is not now [push()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push) works.

`arr.push(something)`

Also, I’m not sure you meant to push the index, but the element `push(arr[index])`

---

<div class="post-metadata">

**Author:** ![colfah](https://avatars.discourse-cdn.com/v4/letter/c/a9adbd/32.png) [@colfah](https://forum.freecodecamp.org/u/colfah)\
**Post date:** [June 20, 2019, 12:38pm UTC](https://forum.freecodecamp.org/t/drop-it-why-cant-i-test-for-elements-that-pass-function-and-push-them-to-new-array/287583/3 "2019-06-20T12:38:35Z")

</div>

Thank you

Back on track now

---

<div class="post-metadata">

**Author:** ![colfah](https://avatars.discourse-cdn.com/v4/letter/c/a9adbd/32.png) [@colfah](https://forum.freecodecamp.org/u/colfah)\
**Post date:** [June 20, 2019, 12:55pm UTC](https://forum.freecodecamp.org/t/drop-it-why-cant-i-test-for-elements-that-pass-function-and-push-them-to-new-array/287583/4 "2019-06-20T12:55:42Z")

</div>

SOLVED: I understand it now.

The challenge isn’t looking for all values that pass the condition in the function. It wants the first element of arr to pass the condition in the function only.

My code is working for all of the examples except the second and the last.

I think I don’t properly understand the challenge.

In the second example, why should the answer include zero in the array when the condition in the function is return n === 1 ?

Similarly, in the final example, why should the answer include 2 in the array when the condition in the function is return n \> 2 ?

```auto
function dropElements(arr, func) {
  
  
  // for loop with if statement to splice

let answer = [];

  for(let i = 0; i < arr.length; i++){
    if (func(arr[i])){
        answer.push(arr[i]);
    }
  }
  return answer;
}

```

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 25, 2021, 12:37pm UTC](https://forum.freecodecamp.org/t/drop-it-why-cant-i-test-for-elements-that-pass-function-and-push-them-to-new-array/287583/5 "2021-01-25T12:37:09Z")

</div>

Closed on January 25, 2021, 12:37pm UTC


