-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtakeUntil.js
36 lines (32 loc) · 1.04 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
//const variable defined with an anonymous function
const takeUntil = function(array, callback) {
const results []; //empty array to store future results
for (let element of array) { //loop
if (callback(element)) { // calling callback function, passing element
return results; //exit loop - give result of array
}
results.push(element); //push elements of array
}
return results;
}
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}`);
}
};
// Test cases
assertArraysEqual(takeUntil(data1, x => x < 0), [1, 2, 5, 7, 2]);
assertArraysEqual(takeUntil(data2, x => x === ','), ["I've", "been", "to", "Hollywood"])