-
Notifications
You must be signed in to change notification settings - Fork 0
/
without.js
49 lines (40 loc) · 1.33 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
const eqArrays = function (arr1, arr2) {
if (arr1.length !== arr2.length) {
return false;
}
for (let i = 0; i < arr1.length; i++){
if (arr1[i] !== arr2[i]) {
return false
}
} return true;
};
const assertArraysEqual = function (arr1, arr2) {
if (eqArrays(arr1, arr2)) {
return (`✅✅✅ Assertion Passed: ${arr1} === ${arr2}`);
} else {
return (`🔴🔴🔴 Assertion Failed: ${arr1} !== ${arr2}`);
}
}
const without = function(source, itemsToRemove) {
let newArr = [];
for (let i = 0; i < source.length; i ++) {
let removeWord = false;
for (let j = 0; j < itemsToRemove.length; j++) {
if (source[i] === itemsToRemove[j]) {
removeWord = true;
}
}
if (removeWord === false) {
newArr.push(source[i]);
}
}
return newArr;
}
console.log(without([1, 2, 3], [1])) // => [2, 3]
console.log(assertArraysEqual(without([1, 2, 3], [1]), [2, 3]));
console.log(without(["1", "2", "3"], [1, 2, "3"])) // => ["1", "2"]
//Below codes are for making sure the original assertArraysEqual function is not alterd
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"]);