Drop it - Help Needed with Getting Second and Last Tests to Pass

How do I get the second and last tests for the challenge to pass? I can’t figure it out. Would someone please me out here?

My code so far

function dropElements(arr, func) {
  for (const num of arr) {
    if (!func(num)) {
      arr.splice(arr.indexOf(num), 1);
  return arr;

console.log(dropElements([0, 1, 0, 1], function(n) {return n === 1; }));

Your browser information:

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

Challenge: Drop it

Link to the challenge:


Currently your function removes all elements from the array that return false for the func parameter. Instead, the instructions want you to remove the false elements until you hit the first true element, then return that array.

How do I do that? I tried to do

let index = 0;
do {
  arr = arr.splice(index, 1)
} while (!func(arr[index]));

But with this it says in the console that there’s a potential infinite loop on line 3 (which inside the editor on the site is the line with do on it).

Is there a way to get it to not be an infinite loop, even potentially?

do not mutate the array on which you are iterating… that doesn’t work well usually

there are simpler ways, and this still doesn’t work

do you understand the requirements?
with the [0, 1, 0, 1] array, the test is n === 1, that means that you need to return [1, 0, 1]

  [0, 1, 0, 1]
this is the first element in the array that passes the test (returns true)
you need to return an array like this
 but with everything before the passing element removed,
so you need to return [1, 0, 1]
1 Like

I get what I need to return. What I’m not getting is the “how”.

This is what I have:

function dropElements(arr, func) {
  let index = 0;
  let resultArr = [...arr];
  while (!func(arr[index])) {
    resultArr = resultArr.splice(index, 1);

What I missing? resultArr is currently just 0 with that input. Is there a way I could combine the two arrays in such a way that the element that splice took out isn’t included in the resulting array?

do you know how to extract, or copy, a part of an array?

if you know that, you will need to find the index at which the first item returns true, and return the rest of the array

also, splice is not doing what you want

for [0,1,0,1] and n === 1 as test
index = 0
first item is removed, so now it’s [1,0,1]
index = 1
0 still return false, let’s remove it

and so on

issues for your current solution:

  • splice returns the removed elements
    if you fix that
    as splice change an array, index will reference different values on resultArr and arr

So do I just need to a use a different Array method? Or use splice differently?

both can be fine

hint: remember that a return statement returns a value and stop the function

Are you saying I need to return from inside the loop?

Edit: Seems I need to do more than that.

resultArr = arr.slice(index + 1); did it.