/
without.js
55 lines (46 loc) · 1.41 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
const eqArrays = function(arr1, arr2) {
if (arr1.length !== arr2.length) {
return false;
} else {
for (let i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) {
return false;
}
}
}
return true;
};
let assertArraysEqual = function(actual, expected) {
const result = eqArrays(actual, expected);
if (result === true) {
console.log(`✅✅✅ Assertion Passed: ${actual} === ${expected}`);
} else {
console.log(`🛑🛑🛑 Assertion Failed: ${actual} !== ${expected}`);
}
};
const without = function(source, itemsToRemove) {
let output = [];
for (let i = 0; i < source.length; i++) {
if (!isValueInArray(source[i], itemsToRemove)) {
//we are grabbing arrays values that are not the same
output.push(source[i]);
}
}
return output;
};
const isValueInArray = function(value, arr) {
let found = false;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === value) {
found = true;
}
}
return found;
};
//Test Code
assertArraysEqual(without([1, 2, 3], [1]), [2, 3]);
assertArraysEqual(without(["1", "2", "3"], [1, 2, "3"]), ["1", "2"]);
const words = ["hello", "world", "lighthouse"];
without(["hello", "world", "lighthouse"], ["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"]);