-
Notifications
You must be signed in to change notification settings - Fork 0
/
takeUntil.js
55 lines (44 loc) · 1.21 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
49
50
51
52
53
54
55
const assertEqual = function(actual, expected) {
if(actual === expected){
console.log(`${String.fromCodePoint(0x1F354)} Assertion Passed: ${actual} === ${expected}`);
}
else{
console.log(`${String.fromCodePoint(0x1F621)} Assertion Failed: ${actual} !== ${expected}`);
}
};
const assertArraysEqual = function(array1, array2){
let result = false;
if(array1.length !== array2.length){
return false;
}
else{
for(let a in array1){
if(array1[a] !== array2[a]){
return false;
}
else{
result = true;
};
}
}
return result;
}
const takeUntil = function(array, callback) {
const newArray = [];
for(const item of array){
if(callback(item)){
break;
}
newArray.push(item);
}
return newArray;
}
// const data1 = [1, 2, 5, 7, 2, -1, 2, 4, 5];
// const results1 = takeUntil(data1, x => x < 0);
// console.log(results1);
// console.log('---');
// const data2 = ["I've", "been", "to", "Hollywood", ",", "I've", "been", "to", "Redwood"];
// const results2 = takeUntil(data2, x => x === ',');
// console.log(results2);
// console.log(assertArraysEqual(takeUntil(data1, x => x < 0), [ 1, 2, 5, 7, 2 ]));
module.exports = takeUntil;