# Drop it: solved but not sure how(?!)

**URL:** <https://forum.freecodecamp.org/t/drop-it-solved-but-not-sure-how/175715>\
**Category:** General\
**Created:** [February 20, 2018, 8:37pm UTC](https://forum.freecodecamp.org/t/drop-it-solved-but-not-sure-how/175715 "2018-02-20T20:37:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kkoutoup](https://avatars.discourse-cdn.com/v4/letter/k/ac91a4/32.png) [@kkoutoup](https://forum.freecodecamp.org/u/kkoutoup)\
**Post date:** [February 20, 2018, 8:37pm UTC](https://forum.freecodecamp.org/t/drop-it-solved-but-not-sure-how/175715/1 "2018-02-20T20:37:08Z")

</div>

So I solved this challenge but there’s something in my code that I don’t understand why/how it’s working(!). The idea is to loop through the array until I find ‘first’ i.e. the first occurrence where `func` returns true and then use this to slice the array or return an empty array if the `func` returns -1.

What I don’t get is how the index of the first value that makes the function return true is transposed from inside the while loop to the `first` variable underneath. I didn’t store it inside the while loop to call it afterwards (and even if I did, after the `break;` statement it would be unreachable right?).

In other words how can `arr.indexOf(arr[i])` be the specific index that made the loop stop? Couldn’t it be any index of any value inside the array?

Does this make sense?

Thanks

**Your code so far**

```auto
  var i=0;
  while(i<arr.length){
    if(func(arr[i])){
      break;
    }
    i++;
  }
  var first = arr.indexOf(arr[i]);
  return first===-1?[]:arr.slice(first);
}

dropElements([1, 2, 3, 4], function(n) {return n > 5;});

```

**Your browser information:**

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

**Link to the challenge:**  
[https://www.freecodecamp.org/challenges/drop-it](https://www.freecodecamp.org/challenges/drop-it)

---

<div class="post-metadata">

**Author:** ![camperextraordinaire](https://avatars.discourse-cdn.com/v4/letter/c/5f9b8f/32.png) [@camperextraordinaire](https://forum.freecodecamp.org/u/camperextraordinaire)\
**Post date:** [February 20, 2018, 8:52pm UTC](https://forum.freecodecamp.org/t/drop-it-solved-but-not-sure-how/175715/2 "2018-02-20T20:52:24Z")

</div>

> [@kkoutoup](#):
>
> In other words how can arr.indexOf(arr[i]) be the specific index that made the loop stop? Couldn’t it be any index of any value inside the array?

Because i is tracking the current index of arr before the break is executed. When func returns true, the while loop breaks and i has the index of arr where you should slice. In fact, since i already contains correct index where the slice should start, you could simplify your code and start i at -1 and not have to use indexOf.

```auto
function dropElements(arr,func) {
  var i=-1;
  while(i<arr.length){
    if(func(arr[i])){
      break;
    }
    i++;
  }
  return i===-1?[]:arr.slice(i);
}

```

---

<div class="post-metadata">

**Author:** ![kkoutoup](https://avatars.discourse-cdn.com/v4/letter/k/ac91a4/32.png) [@kkoutoup](https://forum.freecodecamp.org/u/kkoutoup)\
**Post date:** [February 21, 2018, 8:39am UTC](https://forum.freecodecamp.org/t/drop-it-solved-but-not-sure-how/175715/3 "2018-02-21T08:39:50Z")

</div>

cool!

so each time the loop runs, i is updated outside the loop.  
when func turns true, i is already stored and can be used straight away in the last line of code that returns the desired part of the array.

Thanks very much!

---

<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 17, 2021, 8:26pm UTC](https://forum.freecodecamp.org/t/drop-it-solved-but-not-sure-how/175715/4 "2021-01-17T20:26:16Z")

</div>

Closed on January 17, 2021, 8:26pm UTC


