Skip to content

Instantly share code, notes, and snippets.

@rfprod
Last active April 22, 2017 15:57
Show Gist options
  • Save rfprod/027341fec627218b4246 to your computer and use it in GitHub Desktop.
Save rfprod/027341fec627218b4246 to your computer and use it in GitHub Desktop.
Drop It
function drop(arr, func) {
var passArr = [];
var returnArr = [];
var startValue = 0;
var endValue = 0;
var endValuePosition = 0;
passArr = arr.filter(func);
if (passArr.length > 0){
startValue = passArr[0];
endValue = passArr[passArr.length-1];
endValuePosition = arr.indexOf(endValue);
returnArr = arr;
if (endValuePosition == arr.length-1){
endValuePosition = 0;
}
for (var i=0;i<returnArr.length;i++){
if (startValue == returnArr[0]){
returnArr = returnArr;
}else{
returnArr.shift();
}
}
if (endValuePosition > 0){
for(var z=endValuePosition;z<arr.length;z++){
returnArr.push(arr[z]);
}
}
}
return returnArr;
}
drop([1, 2, 3, 7, 4], function(n) {return n > 3;});

Drop it

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

A script by V.

License.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment