UNPKG

@samnarduzzi/lotide

Version:

Lotide proejct containf files, test code files, index, and descriptions for all files

43 lines (38 loc) 1.42 kB
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}`); } }; const takeUntil = function(array, callback) { const result = []; // empty array to store the items that pass the test of callback function for (let item of array) { // for of loop if (callback(item) === false) { // checks if the callback function returns a truthy value. IF returns false then it will push the current item to the result array. IF truthy then the callback function returns truthy then it passed and will stop adding new items to result array and break the loop. result.push(item); } else { return result; // result array contains all items that were added before the callback function returned truthy. } } }; const data1 = [1, 2, 5, 7, 2, -1, 2, 4, 5]; const results1 = takeUntil(data1, x => x < 0); console.log(results1); const data2 = ["I've", "been", "to", "Hollywood", ",", "I've", "been", "to", "Redwood"]; const results2 = takeUntil(data2, x => x === ','); console.log(results2);