generated from jfarmer/template-javascript
-
Notifications
You must be signed in to change notification settings - Fork 8
/
select.js
32 lines (26 loc) · 941 Bytes
/
select.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
/**
* Given an array and a function, returns a new array containing the elements from
* the input array for which the function returns true.
*
* @example
* select([1, 2, 3, 4, 5, 6], isEven); // => [2, 4, 6]
*
* @param {any[]} collection - An array containing anything
* @param {function} predicate - A function that returns `true` or `false`
* @returns {array} A new array containing the elements of `collection`
* for which `predicate` returns `true`
*/
function select(collection, predicate) {
// See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push
let results = [];
for (let item of collection) {
// This is your job. :)
}
return results;
}
if (require.main === module) {
console.log('Running sanity checks for select:');
// Add your own sanity checks here.
// How else will you be sure your code does what you think it does?
}
module.exports = select;