-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.js
More file actions
63 lines (44 loc) · 1.66 KB
/
map.js
File metadata and controls
63 lines (44 loc) · 1.66 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
46
47
48
49
50
51
52
53
54
55
56
// map FUNCTION IMPLEMENTATION; The map function will take in two arguments - an array to map, and a callback funtion - it will return a new array based on the results of the callback function
const map = function(array, callback) {
const results = [];
for (let item of array) {
results.push(callback(item));
}
return results;
};
module.exports = map;
// // TESTING
// // eqArrays FUNCTION
// const eqArrays = function(array1, array2) {
// let assertion = true;
// if (array1.length !== array2.length) {
// assertion = false;
// return assertion;
// }
// for (let i = 0; i < array1.length; i++) {
// if (array1[i] !== array2[i]) {
// assertion = false;
// return assertion;
// }
// }
// return assertion;
// };
// // assertArraysEqual FUNCTION
// const assertArraysEqual = function(actualArray, expectedArray) {
// let arraysEqual = eqArrays(actualArray, expectedArray);
// if (arraysEqual) {
// console.log(`🟢🟢🟢 Assertion Passed: ${actualArray} === ${expectedArray}`);
// } else {
// console.log(`🔴🔴🔴 Assertion Failed: ${actualArray} !== ${expectedArray}`);
// }
// };
// // TEST CASES
// const words = ["ground", "control", "to", "major", "tom"];
// const results1 = map(words, word => word[0]);
// assertArraysEqual(results1, ["g", "c", "t", "m", "t"]);
// const pies = ["apple", "custard", "coconut cream"];
// const results2 = map(pies, pie => pie + " pie");
// assertArraysEqual(results2, ["apple pie", "custard pie", "coconut cream pie"]);
// const numbers = [1, 2, 3, 4, 5, 6, 7];
// const results3 = map(numbers, x => x * 5);
// assertArraysEqual(results3, [5, 10, 15, 20, 25, 30, 35]);