Drop it - This is driving me crazy

Tell us what’s happening:

Hi. Freecodecampers

i got dropElements([1, 2, 3, 9, 2], function(n) {return n > 2;}) should return [3, 9, 2]. but it’s quite clear that the last element on the suggested returned array does not meet the func criteria (2 > 2 ?!).

Did i miss something?

Your code so far


function dropElements(arr, func) {
    // Drop them elements.
    return arr.filter(element => func(element));
  }
  
console.log(dropElements([1, 2, 3, 4], function(n) {return n >= 3;})); 
console.log(dropElements([1, 2, 3, 9, 2], function(n) {return n > 2;}));

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

Link to the challenge:

The challenge is asking you to go through the array dropping elements until the function returns true, as in iterate through the array and stop iterating at a certain point.

filter will not work here, filter drops any elements that do not return true. And you cannot stop it iterating, it will always go over the entire array regardless.

Wow, thanks. I miss the “once the condition is satisfied” part, and of course the map/reduce/filter methods does not have any break method.

Thanks for the quick replay.

1 Like