/
eqObjects.js
106 lines (79 loc) · 2.1 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const assertEqual = function (actual, expected) {
if (actual === expected) {
console.log(`✅✅✅ Assertion Passed: ${actual} === ${expected}`);
} else {
console.log(`🛑🛑🛑 Assertion Failed: ${actual} !== ${expected}`);
}
return;
};
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;
}
//function that takes in two objects
const eqObjects = function (object1, object2) {
// establish that we need the keys of each object
const object1Keys = Object.keys(object1) // [c, d]
const object2Keys = Object.keys(object2) // [c, d]
//console.log(object1Keys, object2Keys); ==> Test
// compare length of keys
if (object1Keys.length !== object2Keys.length) {
return false;
}
// compare values of keys
for (let key of object1Keys) {
const object1Value = object1[key] // ["2", 3]
const object2Value = object2[key] // ["2", 3, 4]
//console.log(object1key);
console.log(object1Value);
console.log(object2Value);
// Check if value is an array and if they are equal
if ((Array.isArray(object1Value) && Array.isArray(object2Value)) && !eqArrays(object1Value, object2Value)) {
return false;
}
//console.log("fail when comparing values")
}
if (object1Value !== object2Value) {
return false;
}
return true;
}
const cd = {
c: "1",
d: ["2", 3]
};
const dc = {
d: ["2", 3],
c: "1"
};
assertEqual(eqObjects(cd, dc), true); // => true
const cd2 = {
c: "1",
d: ["2", 3, 4]
};
assertEqual(eqObjects(cd, cd2), false); // => false
//console.log(eqArrays(["2", 3], ["2", 3, 4]));
//const ab = {
// a: "1",
// b: "2"
//};
//const ba = {
// b: "2",
// a: "1"
//};
//eqObjects(ab, ba); // ==> true
//const abc = {
// a: "1",
// b: "2",
// c: "3"
//};
//eqObjects(ab, abc); //==> false
//assertEqual(eqObjects(ab, abc), false);
//assertEqual(eqObjects(ab, ba), true);