-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
30 lines (24 loc) · 955 Bytes
/
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
const assertArraysEqual = function(firstArray, secondArray) {
let arrayMaxLength = firstArray.length;
if (secondArray.length > arrayMaxLength) {
arrayMaxLength = secondArray.length;
}
for (let i = 0; i < arrayMaxLength; i++) {
if (firstArray[i] !== secondArray[i]) {
console.log(`🛑🛑🛑 Assertion Failed, [${firstArray}] is not equal to [${secondArray}]`);
return;
}
}
console.log(`✅✅✅ Assertion Passed, [${firstArray}] is equal to [${secondArray}]`);
};
const map = function(array, callback) {
const results = [];
for (let item of array) {
results.push(callback(item));
}
return results;
};
const words = ["ground", "control", "to", "major", "tom"];
assertArraysEqual(map(words, word => word[0]), ["g", "c", "t", "m", "t"]);
assertArraysEqual(map(words, word => word[1]), ["r", "o", "o", "a", "o"]);
assertArraysEqual(map(words, word => word[0].toUpperCase()), ["G", "C", "T", "M", "T"]);