-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.js
39 lines (34 loc) · 1.03 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
// ASSERT
const eqArrays = function(arrOne, arrTwo) {
if (arrOne.length !== arrTwo.length) {
return false;
}
for (i = 0; i < arrOne.length; i++) {
if (arrOne[i] !== arrTwo[i]) {
return false;
}
}
return true;
};
const assertArraysEqual = function(actual, expected) {
if (eqArrays(actual, expected)) {
console.log(`✅✅✅ Assertion Passed: ${(actual)} === ${(expected)}`);
} else {
console.log(`🛑🛑🛑 Assertion Failed: ${(actual)} !== ${(expected)}`);
}
};
// ACTUAL FUNCTION
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]);
assertArraysEqual(results1, ['g', 'c', 't', 'm', 't']);
const results2 = map(words, word => word.length);
assertArraysEqual(results2, [6, 7, 2, 5, 3]);
const results3 = map(words, word => word[5]);
assertArraysEqual(results3, ['d', 'o', undefined, undefined, undefined]);