-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrop_It.js
More file actions
45 lines (41 loc) · 1.35 KB
/
Drop_It.js
File metadata and controls
45 lines (41 loc) · 1.35 KB
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
function dropElements(arr, func) {
while (arr.length > 0) {
if (func(arr[0])) { break; }
arr.shift();
}
return arr;
}
/*
function dropElements(arr, func) {
var i = 0;
while (i === 0 && arr.length > 0) {
if (!func(arr[0]) && arr.length > 0) {
arr.shift();
}
else {
i++;
}
}
return arr;
}
*/
/*
function dropElements(arr, func) {
return (!func(arr[0]) && arr.length > 0) ? dropElements(arr.slice(1), func) : arr;
}
*/
// Some ES6-ness
// const dropElements = (arr, func) => (!func(arr[0]) && arr.length > 0) ? dropElements(arr.slice(1), func) : arr;
dropElements([1, 2, 3], function(n) {return n < 3; });
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].