-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
30 lines (26 loc) · 906 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 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 assertArraysEqual = function (array1, array2) {
for (let i = 0; i < array1.length; i++ ) {
if (array1[i] !== array2[i]) {
console.log(`'${array1}' and '${array2}' are not same ❌`);
return false;
}
}
console.log(`'${array1}' and '${array2}' are the same✅`);
};
const assertEqual = function(actual, expected) {
if (actual === expected) {
console.log(`The '${actual}' and the '${expected}' are the same✅`);
} else console.assert(actual === expected, "❌");
};
const results1 = map(words, word => word[0]);
console.log(assertArraysEqual(results1, words));
console.log(assertArraysEqual(results1, []));
console.log(assertArraysEqual(results1, [1, 3 ,4,5]))