-
Notifications
You must be signed in to change notification settings - Fork 0
/
without.js
37 lines (33 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
const assertArraysEqual = function(firstArray, secondArray) {
for (let i = 0; i < firstArray.length; i++) {
if (firstArray[i] !== secondArray[i]) {
console.log(`🛑🛑🛑 Assertion Failed, [${firstArray}] is not equal to [${secondArray}]`);
return;
}
}
console.log(`✅✅✅ Assertion Passed, [${firstArray}] is equal to [${secondArray}]`);
};
const without = function(inputArray, removedArray) {
let outputArray = inputArray.slice(0);
let matchs = [];
for (let i = 0; i < inputArray.length; i++) {
for (let j = 0; j < removedArray.length; j++) {cd
if (inputArray[i] === removedArray[j]) {
matchs.push(i);
}
}
}
for (let i = (matchs.length - 1); i >= 0; i--) {
outputArray.splice(matchs[i], 1);
}
return outputArray;
};
assertArraysEqual(without([1, 2, 3], [1]), [2, 3]);
assertArraysEqual(without(["1", "2", "3"], [1, 2, "3"]), ["1", "2"]);
assertArraysEqual(without(["1", "2", "3", "4"], [1, 2, "3"]), ["1", "2", "4"]);
assertArraysEqual(without([2, 1, 2, 3], [1, 2]), [3]);
assertArraysEqual(without([3, 2, 1, 2, 3, 4], [1, 2]), [3,3]);
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"]);