-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
31 lines (30 loc) · 1.12 KB
/
Copy pathscript.js
File metadata and controls
31 lines (30 loc) · 1.12 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
/**
* Drop the elements of an array (first argument), starting from the front, until the predicate (second argument) returns true.
* The second argument, func, is a function you'll use to test the first elements of the array to decide if you should drop it or not.
* Return the rest of the array, otherwise return an empty array.
*
* @param {Array} arr
* @param {Function} func
* @returns {Array}
*/
function dropElements(arr, func) {
// Drop them elements.
while (!func(arr[0]) && arr.length > 0) {
arr.shift()
}
return arr
}
console.log(
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].
)