-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
36 lines (32 loc) · 1.12 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
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(actual, expected) {
let goodEmoji = String.fromCodePoint(0x1F525);
let badEmoji = String.fromCodePoint(0x1F608);
if (eqArrays(actual,expected)) {
console.log(`${goodEmoji}${goodEmoji}${goodEmoji}Assertion Passed: ${actual} === ${expected}`);
} else {
console.log(`${badEmoji}${badEmoji}${badEmoji}Assertion Failed: ${actual} !== ${expected}`);
}
};
const eqArrays = function(array1, array2) {
if (array1.length !== array2.length) {
return false;
} else {
for (let index in array1) {
if (array1[index] !== array2[index]) {
return false;
}
}
return true;
}
};
assertArraysEqual(map(words, word => word[0]), [ 'g', 'c', 't', 'm', 't' ]);
assertArraysEqual(map(words, word => word[0] + word[1]), [ 'gr', 'co', 'to', 'ma', 'to' ]);
assertArraysEqual(map(words, word => word += 'map'), ['groundmap', 'controlmap', 'tomap', 'majormap','tommap']);