@kowo0403hk/lotide
Version:
lotide library by LHL Bootcamp student
54 lines (42 loc) • 1.41 kB
JavaScript
// implement the takeUntil function, which will take in two params: 1. array, 2. callback
const assertArraysEqual = function(arr1, arr2) {
if (eqArrays(arr1, arr2)) {
console.log(`\u2705\u2705\u2705 Assertion Passed: ${arr1} === ${arr2}`);
} else {
console.log(`\u26d4\u26d4\u26d4 Assertion Failed: ${arr1} !== ${arr2}`);
}
};
const eqArrays = function(arr1, arr2) {
// eliminate edge cases: arrays are not having same length, and either of the input is not array
if (arr1.length !== arr2.length || !(Array.isArray(arr1)) || !(Array.isArray(arr2))) {
return false;
}
for (let i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) {
return false;
}
}
return true;
};
const takeUntil = (array, callback) => {
const results = [];
for (let item of array) {
if (!(callback(item))) {
results.push(item);
} else return results;
}
};
// test case
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);
// Expected Output
// [ 1, 2, 5, 7, 2 ]
// --
// [ 'I\'ve', 'been', 'to', 'Hollywood' ]
assertArraysEqual(results1, [1, 2, 5, 7, 2]);
assertArraysEqual(results2, ["I've", "been", "to", "Hollywood"]);