-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheqObjects.js
52 lines (44 loc) · 1.49 KB
/
eqObjects.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
// Returns true if both objects have identical keys with identical values.
// Otherwise you get back a big fat false!
const assertEquals = function(actual, expected) {
if (actual === expected) {
console.log(`✔✔ Assertion Passed: ${actual} === ${expected}`);
} else {
console.log(`❌ Assertion Failed: ${actual} !== ${expected}`);
}
};
const eqArrays = function (array1, array2) {
if (array1.length !== array2.length) {
return false;
}
for (let i = 0; i < array1.length; i++) {
if (array1[i] !== array2[i]) {
return false;
}
}
return true;
};
const eqObjects = function(object1, object2) {
const array1 = Object.keys(object1);
const array2 = Object.keys(object2);
if (array1.length !== array2.length) {
return false;
}
for (const item of array1) {
if (Array.isArray(object1[item]) && Array.isArray(object2[item])) {
if (!eqArrays(object1[item], object2[item])) {
return false;
}
} else {
if (object1[item] !== object2[item]) {
return false;
}
}
}
return true;
};
const multiColorShirtObject = { colors: ["red", "blue"], size: "medium" };
const anotherMultiColorShirtObject = { size: "medium", colors: ["red", "blue"] };
const longSleeveMultiColorShirtObject = { size: "medium", colors: ["red", "blue"], sleeveLength: "long" };
assertEquals(eqObjects(multiColorShirtObject, anotherMultiColorShirtObject), true);
assertEquals(eqObjects(multiColorShirtObject, longSleeveMultiColorShirtObject), false);