-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
48 lines (40 loc) · 1.16 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
// FUNCTION IMPLEMENTATION
const assertEqual = function(actual, expected) {
if (actual === expected) {
console.log(`${String.fromCodePoint(0x1F600)} Assertion Passed: ${actual} === ${expected}`);
} else {
console.log(`${String.fromCodePoint(0x1F34A)} Assertion Failed: ${actual} !=== ${expected}`);
}
};
const eqArrays = function(a, b) {
if (a.length !== b.length) {
console.log("false");
return false;
} else {
for (let i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) {
console.log("false");
return false;
}
}
console.log("true");
return true;
}
};
const words = ["ground", "control", "to", "major", "tom"];
const map = function(array, callback) {
const results = [];
for (let item of array) {
results.push(callback(item));
}
//console.log(results);
return results;
};
const results1 = map(words, function(word) {
return word[0];
});
console.log(results1);
//[ 'g', 'c', 't', 'm', 't' ]
eqArrays(results1, [ 'g', 'c', 't', 'm', 't' ]); // => true
eqArrays(results1, [ 'g', 'c', 't', 'm']); // => false
assertEqual(eqArrays(results1, [ 'g', 'c', 't', 'm', 't' ]), true); // => should pass