-
Notifications
You must be signed in to change notification settings - Fork 0
/
without.js
49 lines (47 loc) · 1.52 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
// FUNCTION IMPLEMENTATION
const without = function(original, unwanted) {
let wanted = [];
for (let i in original) {
let temp = true;
for (let j in unwanted) {
if (original[i] === unwanted[j]) {
temp = false;
break;
}
}
if (temp) {
wanted.push(original[i]);
}
}
return wanted;
};
const assertArraysEqual = function(actual, expected) {
let goodEmoji = String.fromCodePoint(0x1F525);
let badEmoji = String.fromCodePoint(0x1F608);
if (eqArrays(actual,expected)) {
console.log(`${goodEmoji}${goodEmoji}${goodEmoji}Assertion Passed: ${actual} === ${expected}`);
} else {
console.log(`${badEmoji}${badEmoji}${badEmoji}Assertion Failed: ${actual} !== ${expected}`);
}
};
const eqArrays = function(array1, array2) {
if (array1.length !== array2.length) {
return false;
} else {
for (let index in array1) {
if (array1[index] !== array2[index]) {
return false;
}
}
return true;
}
};
// TEST CODE
const words = ["hello", "world", "lighthouse"];
assertArraysEqual(without(words, ["lighthouse"]), ["hello", "world"]); // 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"]);
assertArraysEqual(without([1, 2, 3], [1]), [2,3]);
assertArraysEqual(without(["1", "2", "3"], [1, 2, "3"]), ["1", "2"]);
assertArraysEqual(without(["1", "2", "3"], []), ["1", "2", "3"]);
assertArraysEqual(without([1, 2, 3, 2, 1], [1,2]), [3]);