-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
51 lines (42 loc) · 1.8 KB
/
map.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
// Need explanation on below - W1D4_implementMap
// Note that the code above is only showing the map function, but our full map.js file will contain other code outside the function that is not shown above.
// The important bit above is that our map (higher-order) function can call the callback function. This callback function is the one that is passed in as its second argument and conveniently called callback.
// Since map is passing in the original item to it, our test code which calls map knows to pass in a callback function which accepts that one and only argument.
const words = ["ground", "control", "to", "major", "tom"];
const map = function(array, callback) {
const results = [];
for (let item of array) {
results.push(callback(item));
}
return results;
};
const results1 = map(words, word => word[0]);
console.log(results1);
const eqArrays = function(first, second) {
if (first.length !== second.length) {
return false;
} else {
let result = false;
for (let i = 0; i < first.length; i++) {
if (first[i] !== second[i]) {
result = false;
} else {
result = true;
}
}
return result;
}
};
const assertArraysEqual = function(first, second) {
if (eqArrays(first, second)) {
console.log(`🤩🤩🤩 Assertion Passed: [${first}] === [${second}]`);
} else {
console.log(`😡😵💫😰 Assertion Failed: [${first}] !== [${second}]`);
}
};
const testArray2 = [1, 2, 3];
const testArray3 = [3, 2, 1];
assertArraysEqual(map(words, word => word[0]), ['g', 'c', 't', 'm', 't']);
assertArraysEqual(map(testArray2, num => num * 2), [2, 4, 6]);
assertArraysEqual(map(testArray3, num => num > 2), [true, false, false]);
//Need further clarify on 'Write test cases using at least three different ways of using map.' despite boilerplate tests passing