-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.js
85 lines (81 loc) · 2.69 KB
/
index.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* Returns the first element in the array that causes the callback to return `true`. Otherwise, returns `undefined`.
* @param {*[]} array - An array of elements. Could be anything!
* @param {function} callback - A callback that accepts a single argument. Returns a value.
* @returns {*|undefined} The first element that causes the callback to return a truthy value. Otherwise, returns `undefined`.
*
* EXAMPLE:
* find([1, 2, 3], (element) => element > 1);
* //> 2
*
* EXAMPLE:
* find([1, 2, 3], (element) => element < 0);
* //> undefined
*/
function find(array, callback) {
for (let element of array) {
// Write your code here.
}
}
/**
* Returns an array of all elements in the array that cause the callback to return `true`. If the array is empty or no elements cause the callback to return `true`, then return an empty array.
* @param {*[]} array - An array of elements. Could be anything!
* @param {function} callback - A callback that accepts a single argument. Returns a value.
* @returns {*[]} An array of filtered values. Potentially empty.
*
* EXAMPLE:
* filter([1, 2, 3], (element) => element > 1);
* //> [2, 3]
*
* EXAMPLE:
* filter([1, 2, 3], (element) => element < 0);
* //> []
*/
function filter(array, callback) {
const result = [];
for (let element of array) {
// Write your code here.
}
return result;
}
/**
* Returns an array where each element is transformed by the callback. If the array is empty, return an empty array.
* @param {*[]} array - An array of elements. Could be anything!
* @param {function} callback - A callback that accepts a single argument. Returns a value.
* @returns {*[]} An array of transformed elements. The length of this array should be the same as the inputted array.
*
* EXAMPLE:
* map([1, 2, 3], (element) => element + 10);
* //> [11, 12, 13]
*
* EXAMPLE:
* map([], (element) => element < 0);
* //> []
*/
function map(array, callback) {
const result = [];
for (let element of array) {
// Write your code here.
}
return result;
}
/**
* Does not return anything. Passes each element of the array into the callback along with the index and the array, in that order.
* @param {*[]} array - An array of elements. Could be anything!
* @param {function} callback - A callback that accepts three arguments: element, index, and the entire array.
*
* EXAMPLE:
* forEach([10, 20, 30], (element, index, array) => {
* console.log(element, index, array.length)
* });
* //> 10 0 3
* //> 20 1 3
* //> 30 2 3
*/
function forEach(array, callback) {
for (let i = 0; i < array.length; i++) {
// Write your code here.
}
}
// Do not change the code below this line.
module.exports = { find, filter, map, forEach };