-
Notifications
You must be signed in to change notification settings - Fork 0
/
without.js
64 lines (48 loc) · 1.5 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const eqArrays = function(arrayA, arrayB) {
if (arrayA.length !== arrayB.length) {
return false;
} else {
for (let i = 0; i < arrayA.length; i++) {
if (arrayA[i] !== arrayB[i]) {
return false;
}
}
return true;
}
};
const assertArraysEqual = function(actual, expected) {
if (eqArrays(actual, expected)) {
console.log("💚🥬📗 Assertion Passed: ", actual, " === ", expected);
} else {
console.log("🍎🧧🔴 Assertion Failed: ", actual, " !== ", expected);
}
};
const without = function(origArray, removeArray) {
let results = [];
if (eqArrays(origArray, removeArray)) {
return [];
} else {
for (let j = 0; j < origArray.length; j++) {
let check = false;
for (let i = 0; i < removeArray.length; i++) {
if (origArray[j] === removeArray[i]) {
check = true;
}
}
if (check === false) {
results.push(origArray[j]);
}
}
return results;
}
};
// TEST CODE
const words = ["hello", "world", "lighthouse"];
const newWords = 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"]);
console.log(newWords); // => ['hello', 'world']
console.log(without([1, 2, 3], [1])); // => [2, 3]
console.log(without(["1", "2", "3"], [1, 2, "3"])); // => ["1", "2"]
console.log(without(["a", "c"], ["a", "c"]));
module.exports = without;