-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
48 lines (44 loc) · 1.58 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
/******BEGIN PASTED FUNCTIONS assertArraysEqual and eqArrays******/
const assertArraysEqual = function(actualInput, ExpectedInput) {
if (eqArrays(actualInput, ExpectedInput) === true) {
//if input arrays evaluate to true through the eqarrays fx, log:
console.log(`✅ Assertion Passed: ${actualInput} === ${ExpectedInput}`);
} else {
//otherwise, log:
console.log(`🛑 Assertion Failed: ${actualInput} !== ${ExpectedInput}`);
}
};
//function that takes in two arrays
const eqArrays = function(array1, array2) {
//if arrays are not the same length return false
if (array1.length !== array2.length) {
return false;
}
//if arrays were equal lengths, iterate through with for loop
for (let i = 0; i < array1.length; i++) {
//if not exactly equal arrays return false
if (array1[i] !== array2[i]) {
return false;
}
}
//if above cases are not relevant, return true
return true;
};
/******END OF PASTED FUNCTIONS******/
const map = function(array, callback) {
const results = [];
for (let item of array) {
results.push(callback(item));
}
return results;
};
//TEST CASES:
// Write test cases using at least three different ways of using map.
// Think about the different scenarios that make sense to test.
// Be sure to use assertArraysEqual to compare the results from map with expected array results.
// console.log('TEST 1');
// const words = ["ground", "control", "to", "major", "tom"];
// const results = map(words, word => word[0]);
// // console.log(results);
// assertArraysEqual(results, map(words, word => word[0]));
module.exports = map;