-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwithout.js
48 lines (44 loc) · 1.34 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
//Helper Function that compare the values of two arrays
const eqArrays = function (array1, array2) {
let flagFalse = 0;
if (array1.length !== array2.length) {
flagFalse = 1;
} else {
for (let i = 0; i < array1.length; i++) {
if (array1[i] !== array2[i]) {
flagFalse = 1;
}
}
if (flagFalse === 1) {
return false;
} else {
return true;
}
}
};
//Function to check array using helper funtion eqArrays and log the message with input array values
const assertArraysEqual = function (array1, array2) {
if (eqArrays(array1, array2)) {
console.log(`No Items found to remove as [${array1}] === [${array2}]`);
} else {
console.log(`Items to be removed from source are: [${without(array1, array2)}]`);
}
};
const without = function (source, itemToRemove) {
let itemRemoveArr = [];
for (let i = 0; i < source.length; i++) {
if (source[i] !== itemToRemove[i]) {
itemRemoveArr.push(source[i]);
}
}
return itemRemoveArr;
};
//Test Run
assertArraysEqual([1, 2, 3], [1]);
assertArraysEqual(["1", "2", "3",], [1, 2, "3"]);
assertArraysEqual(["1", "2", "3",], []);
assertArraysEqual([], [1, 2, "3"]);
//To check whether input Array remains same
const words = ["hello", "world", "lighthouse"];
(without(words, ["lighthouse"]));
assertArraysEqual(words, ["hello", "world", "lighthouse"]);