Bonfire 34: Drop it

TASK: Drop the elements of an array (first argument), starting from the front, until the predicate (second argument) returns true.

This one passes the test, but I feel like I cheated, since it does not, strictly speaking, do what was requested.


function drop(arr, func) {

return arr.filter(func);
  
  }
 

drop([1, 2, 3], function(n) {return n < 3; });

I tried hard to write a recursive that would actually better fulfil the task, but I couldn’t make it work. This is an alternative that comes closer to doing what is asked:


function drop(arr, func) {

var solution = [];

for (var i=0; i<arr.length; i++) {
if (func(arr[i])) {solution.push(arr[i]);}
}

return solution;
}


drop([1, 2, 3], function(n) {return n < 3; });

Advertisements
Bonfire 34: Drop it

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s