forked from joinpursuit/8-0-higher-order-functions-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
118 lines (106 loc) · 3.7 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* 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 i = 0; i < array.length)
for (let element of array) { // for loop
// Write your code here.
// check is parameters of callback against each element in aray
if(callback(element)){
// return that element that fits callback parameters
return element
}
}
return undefined
}
/**
* 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) { // callback is function being passed as a parameter
const result = [];
//for (let i = 0; i < array.length; i++)
for (let element of array) {
// Write your code here.
// checking if the parameters of callback against elements of array
if(callback(element)) {
//if true push into result
result.push(element)
}
}
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.
// result.push(callback(element))
// }
// return result;
// }
function map(array, callback) { // parameter of callback is in the testfile
const result = [];
for (let element of array) { // loop through array and element = 0 or index
// apply the parameter of callback to the array
result.push(callback(element)) // push the results of that into result[]
}
return result;
}
//push element through array after its been pushed into the call back
/**
* 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.
callback(array[i], i, array) //
//params of
}
}
// Do not change the code below this line.
module.exports = { find, filter, map, forEach };