-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
46 lines (34 loc) · 981 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function eqArrays(array1, array2) {
for (let i = 0; i < array1.length; i++) {
if (array1[i] !== array2[i]) {
return false;
}
}
return true;
}
function assertArraysEqual(array1, array2){
let equalLength = array1.length === array2.length;
let result = true;
for (let i = 0; i < array1.length; i++) {
if (array1[i] !== array2[i])
result = false;
}
if (result && equalLength){
console.log('Good job!');
}
else {
console.log('Not the same');
}
}
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;
}
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[2]), ['o', 'n', '', 'j', 'm']);
module.exports = map;