-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtakeUntil.js
48 lines (42 loc) · 1.28 KB
/
takeUntil.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
// FUNCTION IMPLEMENTATION
// Takes from an array until the callback funcion is true, then returns the array up to that point
const takeUntil = function(array, callback) {
const results = [];
for (let item of array) {
if (callback(item)) {
return results;
} else {
results.push(item);
}
}
return results;
}
module.exports = takeUntil;
/* Test Code
const assertArraysEqual = function(actual, expected) {
if (eqArrays(actual, expected)) {
console.log(`✅✅✅ Assertion Passed: [${actual}] === [${expected}]`);
} else {
console.log(`🛑🛑🛑 Assertion Failed: [${actual}] !== [${expected}]`);
}
};
const eqArrays = function(arrayOne, arrayTwo) {
if (arrayOne.length === arrayTwo.length) {
for (let i = 0; i < arrayOne.length; i++) {
if (arrayOne[i] !== arrayTwo[i]) {
return false;
}
}
}
if (arrayOne.length !== arrayTwo.length) {
return false;
}
return true;
};
const data1 = [1, 2, 5, 7, 2, -1, 2, 4, 5];
const results1 = takeUntil(data1, x => x < 0);
assertArraysEqual(results1, [ 1, 2, 5, 7, 2 ]);
const data2 = ["I've", "been", "to", "Hollywood", ",", "I've", "been", "to", "Redwood"];
const results2 = takeUntil(data2, x => x === ',');
assertArraysEqual(results2, ["I've", "been", "to", "Hollywood" ]);
*/