-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtakeUntil.js
More file actions
65 lines (58 loc) · 2 KB
/
Copy pathtakeUntil.js
File metadata and controls
65 lines (58 loc) · 2 KB
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
56
57
58
59
60
61
62
63
64
65
const takeUntil = function(array, callback) {
let results = [];
for (let item in array) {
if (callback(array[item]) === true) {
break;
} else {
results.push(array[item]);
}
} 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);
/*
****Output****
[ 1, 2, 5, 7, 2 ]
---
[ "I've", 'been', 'to', 'Hollywood' ]
*/
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(array1, array2) {
if (eqArrays(array1, array2)) {
console.log(`✅✅✅ Assertion Passed: ${array1} === ${array2}`);
} else {
console.log(`🛑🛑🛑 Assertion Failed: ${array1} !== ${array2}`);
}
};
//Test Cases:
assertArraysEqual(results1, [ 1, 2, 5, 7, 2 ]);
assertArraysEqual(results2, [ "I've", 'been', 'to', 'Hollywood' ]);
assertArraysEqual(results1, [ "I've", 'been', 'to', 'Hollywood' ]);
assertArraysEqual(takeUntil(["bread", "egg", "butter"], item => item), ["bread", "egg"]);
assertArraysEqual(takeUntil(["bread", "egg", "butter"], item => item), ["bread", "egg", "butter"]);
assertArraysEqual(takeUntil(["bread", "egg", "butter"], item => item), ["bread", "egg", "butter", "pancake"]);
/*
****Output****
✅✅✅ Assertion Passed: 1,2,5,7,2 === 1,2,5,7,2
✅✅✅ Assertion Passed: I've,been,to,Hollywood === I've,been,to,Hollywood
🛑🛑🛑 Assertion Failed: 1,2,5,7,2 !== I've,been,to,Hollywood
🛑🛑🛑 Assertion Failed: bread,egg,butter !== bread,egg
✅✅✅ Assertion Passed: bread,egg,butter === bread,egg,butter
🛑🛑🛑 Assertion Failed: bread,egg,butter !== bread,egg,butter,pancake
*/