## Thursday, July 23, 2020

### Drop it

From: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/drop-it

Intermediate Algorithm Scripting: Drop it

Given the array arr, iterate through and remove each element starting from the first element (the 0 index) until the function func returns true when the iterated element is passed through it.

Then return the rest of the array once the condition is satisfied, otherwise, arr should be returned as an empty array.

dropElements([1, 2, 3, 4], function(n) {return n >= 3;}) should return [3, 4].

dropElements([0, 1, 0, 1], function(n) {return n === 1;}) should return [1, 0, 1].

dropElements([1, 2, 3], function(n) {return n > 0;}) should return [1, 2, 3].

dropElements([1, 2, 3, 4], function(n) {return n > 5;}) should return [].

dropElements([1, 2, 3, 7, 4], function(n) {return n > 3;}) should return [7, 4].

dropElements([1, 2, 3, 9, 2], function(n) {return n > 2;}) should return [3, 9, 2].

```Array.prototype.indexOfCondition = function (func) {
for (let i = 0; i < this.length; ++i) {
if (func(this[i])) {
return i;
}
}

// it's better to use undefined instead of -1.
// see slice code below what we can do with undefined
return undefined;
};

const dropElements = (arr, func) => arr.slice(arr.indexOfCondition(func) ?? arr.length);

[
dropElements([1, 2, 3, 4], n => n >= 3),
dropElements([0, 1, 0, 1], n => n === 1),
dropElements([1, 2, 3], n => n > 0),
dropElements([1, 2, 3, 4], n => n > 5),
dropElements([1, 2, 3, 7, 4], n => n > 3),
dropElements([1, 2, 3, 9, 2], n => n > 2)
].forEach(result => console.log(result));
```

Output:
```[ 3, 4 ]
[ 1, 0, 1 ]
[ 1, 2, 3 ]
[]
[ 7, 4 ]
[ 3, 9, 2 ]
```