Help with explanation of function in Drop it challenge

Tell us what’s happening:
Hi everyone. I’ve solved this challenge a few different ways, but in these two cases I fail to explaine them. How is it that I can use func here, without passing a parameter? Would that not be “undefined”? How does the code know what to put into “n” in func?.

And then there is the while loop. How does the loop know where to look? Does it take the first argument which happens to be the array? To me the loop just says: while 1, shift the array. But 1 where?

I hope that makes sence :smile:

Thank you

Your code so far

 function dropElements(arr, func) {
  // Drop them elements.

  if (arr.findIndex(func) < 0) {
    return [];
  } else {return arr.slice(arr.findIndex(func))};

// Using shift
while (arr.findIndex(func) ) {
return arr


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/70.0.3538.102 Safari/537.36.

Link to the challenge:

You can pass functions around as if they were values in JS, so:

function(n) {return n === 1}

Is the function. So give it an identifier

var func = function(n) {return n === 1 }

idexOf takes a function as a parameter, and the first argument it takes is a value, and the function returns true or false (it checks that value against something). idexOf runs that function for every value in the array.

So you just need to tell indexOf the function you’re going to give it, and it will try to run that function.

1 Like