-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
56 lines (43 loc) · 1.26 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
49
50
51
52
53
54
55
56
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]);
// console.log(results1);
// for testing...
const assertArraysEqual = function(actualArray, expectedArray) {
let isTrue = 1;
for (let i = 0; i < actualArray.length; i++) {
if (actualArray[i] !== expectedArray[i]) {
isTrue -= 1;
console.log(`🚫🚫🚫 Assertion Failed: ${actualArray} !== ${expectedArray}`);
}
}
if (isTrue) {
console.log(`🚀🚀🚀 Assertion Passed: ${actualArray} === ${expectedArray}`);
}
};
const eqArrays = function(array1, array2) {
if (array1.length !== array2.length) {
return false;
}
for (let i = 0; i < array1.length; i++) {
if (array1[i] !== array2[i]) {
return false;
}
}
for (let i = array2.length -1; i > -1; i--) {
if (array2[i] !== array1[i]) {
return false;
}
}
return true;
};
// test cases:
assertArraysEqual(map(words, word => word[0]), ['g', 'c', 't', 'm', 't'])
const testArr = ['1', 'two', 'three', '4', 'five'];
assertArraysEqual(map(testArr, testChar => testChar[0]), ['1', 't', 't', '4', 'f']);