-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtakeUntil.js
35 lines (25 loc) · 899 Bytes
/
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
//Import functions
const assertArraysEqual = require('./assertArraysEqual');
// Slice of the array with elements taken from the beginning until returns a truthy value
const takeUntil = function(array, callback) {
let results = [];
for (const item of array) {
if (callback(item) !== true) {
results.push(item);
} else {
break;
}
}
return results;
};
module.exports = takeUntil;
//TEST CODE
const data1 = [1, 2, 5, 7, 2, -1, 2, 4, 5];
//const results1 = takeUntil(data1, x => x < 0);
//console.log(results1);
assertArraysEqual(takeUntil(data1, item => item < 0), [1, 2, 5, 7, 2]);
console.log('---');
const data2 = ["I've", "been", "to", "Hollywood", ",", "I've", "been", "to", "Redwood"];
//const results2 = takeUntil(data2, x => x === ',');
//console.log(results2);
assertArraysEqual(takeUntil(data2, x => x === ','), ['I\'ve', 'been', 'to', 'Hollywood']);