Intermediate Algorithm Scripting - Drop it

Tell us what’s happening:
Describe your issue in detail here.

The code here is passing all but 2 of the tests, and I’m pretty sure I understand the reason why based on what the console is spitting out versus what I’m supposed to be getting. It looks like the tests are looking for the first instance of passing the func(element) condition being passed in as the second argument…am I right about that? Because otherwise, my implementation of “filter” should have worked…my issue now is trying to figure out how to filter out only the first instance of the functional argument’s condition. Is my logic just off?? Or am I looking at it wrong?

Your code so far

function dropElements(arr, func) {
// return the array filtered with the conditional statement that (if) => the element passed through the given function is true, return all those elements
  return arr.filter(element => {
    if (func(element) === true) {
      return element;
    } 
  });
}

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

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; rv:109.0) Gecko/20100101 Firefox/112.0

Challenge: Intermediate Algorithm Scripting - Drop it

Link to the challenge:

The callback for .filter() is expected to return a boolean, so the returned value is treated as one. If you return element and element is 0, that’s a falsey value and the element will be removed.

So, would it be possible for me to put another condition in that disregards removing any zeros and then unshift the first element in the array? Or would that be too much hard-coding? I feel like that would be hard coding for that specific case…

When you use the .filter() method, if the callback returns a truthy value, the element is put in the new array and if it returns a falsey value then the element is removed.

The dropElements challenge is basically to create a function that does the same thing as .filter().

Well, it was doing that until it got to the array [0, 1, 0, 1] where it wanted [1, 0, 1] to be returned, but the middle “0” is being seen as a falsey value and is being purged, which is why I thought it might be asking for the “first instance” to be returned. I’ll see if I can add a condition that uses shift to do something about that middle zero.

I actually mis-remembered the specific requirements. If you look at the directions:

… until the function func returns true when the iterated element is passed through it

So I believe that this is saying that as soon as you hit the first true, you should stop evaluating. .filter() might not be the best choice because it doesn’t have an exit condition like that.

ahhh…ok, time to re-work it then. Thanks!