@nknahom/lotide
Version:
Lotide is mini clone of the Lodash (https://lodash.com) library for learning JavaScript. What is Lodash? Lodash is a library that makes JavaScript easier by taking the hassle out of working with arrays, numbers, objects, strings, etc.
41 lines (37 loc) • 1.25 kB
JavaScript
const eqArrays = function (array1, array2) {
let isEqual = false;
if (array1.length === array2.length) {
array1.forEach((element, index) => {
console.log("array2 with index: ", array2[index]);
if (element !== array2[index]) {
isEqual = false;
} else {
isEqual = true;
}
});
}
return isEqual;
};
const assertArraysEqual = function (array1, array2) {
if (eqArrays(array1, array2)) {
console.log(`\u2705 Assertion Passed: ${array1} === ${array2}`);
} else {
console.log(`\u26D4 Assertion Failed: ${array1} !== ${array2}`);
}
};
const without = function (source, itemsToRemove) {
// new copy of source that will be modified
let results = source;
for (let item of itemsToRemove) {
if (results.includes(item)) {
results.splice(source.indexOf(item), 1);
}
}
console.log("results: ", results);
};
const words = ["hello", "world", "lighthouse"];
without(words, ["lighthouse"]); // no need to capture return value for this test case
// Make sure the original array was not altered by the without function
assertArraysEqual(["hello"], ["hello", "world", "lighthouse"]);
without([1, 2, 3], [1]); // => [2, 3]
without(["1", "2", "3"], [1, 2, "3"]); // => ["1", "2"]