-
Notifications
You must be signed in to change notification settings - Fork 0
/
takeUntil.js
45 lines (38 loc) · 1.43 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
const assertArraysEqual = function(firstArray, secondArray) {
if ((eqArrays(firstArray, secondArray)) === true) {
console.log(`✅✅✅ Assertion Passed: ${firstArray} === ${secondArray}`);
} else {
console.log(`🛑🛑🛑 Assertion Failed: ${firstArray} !== ${secondArray}`);
}
};
const eqArrays = function(array1, array2) {
if (array1.length !== array2.length) {
return false;
}
for (let x = 0; x < array1.length; x++) {
if (array1[x] !== array2[x]) {
return false;
}
}
return true;
};
//takeUntil function will take in two parameters (the array, and the callback) and will return a slice of the array with elements taken from the beginning
const takeUntil = function(array, callback) {
const results = []; //store results in empty object
for (let item of array) { //loop through each item in the array
if(!callback(item)) { //while looping through the array, if the item is != to callback function
results.push(item); //push the item into our results
} else {
break; //if the item matches the callback function, break out of the loop
}
}
return results;
}
module.exports = takeUntil;
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);