-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
62 lines (44 loc) · 1.72 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
57
58
59
60
61
62
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;
}
}
return true;
};
const assertArraysEqual = function(actual, expected) {
if (eqArrays(actual, expected)) {
console.log(`✅✅✅ Assertion Passed: ${actual} === ${expected}`);
} else {
console.log(`🛑🛑🛑 Assertion Failed: ${actual} !== ${expected}`);
}
};
const map = function(array, callback) { //here callback is second param
// console.log('array: ', array); //check what prints
// console.log('callback: ', callback);
const results = [];
for (let item of array) {
results.push(callback(item)); //push to empty array the first letter of each string in array
// console.log('item BEFORE: ', item);
// console.log('item AFTER: ', callback(item));
// console.log('---');
}
return results;
}
//TEST 1 - adding each value in array with an integer
const results1 = map([5, 10, 15], arrays => arrays + 5); // array is the var with values, arrays is func name and => after what we want map to process
console.log(results1);
assertArraysEqual(results1, [10, 15, 20]); // adds five to each number in the array
// TEST 2 - multiply x * x
// const results1 = map([1, 2, 3], x => x * x );
// console.log(results1);
// assertArraysEqual(results1, [1, 4, 9] )
// TEST 3 - give back the number of letters in a string
// const string = ["hello", "goodbye", "lighthouse", "labs"]
// const results1 = map(string, strings => strings.length);
// console.log(results1);
// assertArraysEqual(results1, [5, 7, 10, 4])
// TEST 3 -
module.exports = map;