-
Notifications
You must be signed in to change notification settings - Fork 0
/
without.js
50 lines (47 loc) · 1.35 KB
/
without.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const eqArrays = function (arr1, arr2) {
// compare the length of arr1
const length1 = arr1.length;
const length2 = arr2.length;
if (length1 !== length2) {
return false;
// compare the item inside one by one
} else {
for (let i = 0; i < length1; i++) {
if (arr1[i] !== arr2[i]) {
return false;
}
}
return true;
}
};
const assertArraysEqual = function (actual, expected) {
if (eqArrays(actual, expected)) {
console.log(
`✅✅✅ Assertion Passed: ${JSON.stringify(
actual
)} equals ${JSON.stringify(expected)}`
);
} else {
// console.assert(actual === expected, `✅✅✅ ${actual} !== ${expected}`);
console.log(
`🛑🛑🛑 Assertion failed: ${actual} not equals ${JSON.stringify(
expected
)}`
);
}
};
const without = function (source, itemsToRemove) {
let result = [];
// loop items in source
for (const elem of source) {
// if itemsToRemove not includes elems, push into result
if (!itemsToRemove.includes(elem)) {
result.push(elem);
}
}
return result;
};
// 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(words, ["hello", "world", "lighthouse"]);