-
Notifications
You must be signed in to change notification settings - Fork 0
/
eqObjects.js
48 lines (41 loc) · 1.25 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
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;
};
const assertEqual = function(actual, expected) {
if (actual === expected) {
console.log(`✅ ✅ ✅ Assertion Passed: ${actual} === ${expected}`);
} else if (actual !== expected) {
console.log(`❌ ❌ ❌ Assertion Failed: ${actual} !== ${expected}`);
}
};
const eqObjects = function(object1, object2){
if (Object.keys(object1).length === Object.keys(object2).length){
for(let key of Object.keys(object1) ){
if(Array.isArray(object1[key]) && Array.isArray(object2[key])){
if (!eqArrays(object1[key],object2[key])){
return false;
}
}
}
return true;
}
return false
};
const ab = { a: "1", b: "2"};
const ba = { b: "2", a: "1"};
console.log(assertEqual(eqObjects(ab,ba),true));
const abc = { a: "1", b: "2", c: "3"};
console.log(assertEqual(eqObjects(ab,abc),false));
const cd = { c: "1", d: ["2", 3] };
const dc = { d: ["2", 3], c: "1" };
console.log(assertEqual(eqObjects(cd,dc),true));
const cd2 = { c: "1", d: ["2", 3, 4] };
console.log(assertEqual(eqObjects(cd,cd2),false));