-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15-drop-it.js
45 lines (43 loc) · 1.54 KB
/
15-drop-it.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
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].
*/
function dropElements(arr, func) {
let finalArr = [...arr];
for (const val of arr) {
if (func(val)) {
return finalArr;
}
finalArr.splice(0, 1);
}
return [];
}
console.log(dropElements([1, 2, 3], function (n) {
return n < 3;
}));
console.log(dropElements([1, 2, 3, 4], function (n) {
return n >= 3;
}));
console.log(dropElements([0, 1, 0, 1], function (n) {
return n === 1;
}));
console.log(dropElements([1, 2, 3], function (n) {
return n > 0;
}));
console.log(dropElements([1, 2, 3, 4], function (n) {
return n > 5;
}));
console.log(dropElements([1, 2, 3, 7, 4], function (n) {
return n > 3;
}));
console.log(dropElements([1, 2, 3, 9, 2], function (n) {
return n > 2;
}));