function dropElements(arr, func) {
// Drop them elements.
var loop = arr.length;
for(var i=0; i<=loop;i++){
if(func(arr[0])!=true){
arr.shift();
}
}
return arr;
}
dropElements([1, 2, 3, 4], function(n) {return n >= 3;});
Thats how I did it. I though it looked clean, neat and only ran as long as it needed to get to a solution.
Here is my solution:
function dropElements(arr, func) {
var result = [],
test = false,
handler = function(element, index, array) {
if (func(element)) {
test = true;
}
if (test) {
result.push(element);
}
};
arr.forEach(handler);
return result;
}
dropElements([1, 2, 3, 4], function(n) {return n > 5;});
Mine in a few lines:
function dropElements(arr, func) {
while (func(arr[0]) === false) {
arr.shift();
}
return arr;
}
dropElements([1, 2, 3, 4], function(n) {return n >= 3;});
1 Like
Hello coders,
Here’s a slightly different solution from what I’ve seen here:
function dropElements(arr, func) {
// Drop them elements?
for (var i = 0; i < arr[arr.length-1]; i++) {
if(!func(arr[i-i]))
arr.shift();
}
return arr;
}
//Yes we can!
dropElements([1, 2, 3, 9, 2], function(n) {return n > 2;});
2 Likes