-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheqObjects.js
39 lines (39 loc) · 1.63 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
/* eslint-disable linebreak-style */
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(obj1, obj2) {
let obj1Keys = Object.keys(obj1);
let obj2Keys = Object.keys(obj2);
if (obj1Keys.length !== obj2Keys.length) {
return false;
}
for (let key in obj1) {
let property1 = obj1[key];
let property2 = obj2[key];
if (Array.isArray(property1) && Array.isArray(property2)) {
for (let i = 0; i < property1.length; i++) {
if (property1[i] !== property2[i]) {
return false;
}
}
} else {
if (property1 !== property2) {
return false;
}
}
} return true;
};
const shirtObject = { color: "red", size: "medium" };
const anotherShirtObject = { size: "medium", color: "red" };
const multiColorShirtObject = { colors: ["red", "blue"], size: "medium" };
const anotherMultiColorShirtObject = { size: "medium", colors: ["red", "blue"] };
const longSleeveMultiColorShirtObject = { size: "medium", colors: ["red", "blue"], sleeveLength: "long" };
assertEqual(eqObjects(shirtObject , anotherShirtObject),true); // => true
assertEqual(eqObjects(longSleeveMultiColorShirtObject , anotherShirtObject),false); // => false
assertEqual(eqObjects(multiColorShirtObject , anotherMultiColorShirtObject), true);//should pass
assertEqual(eqObjects(multiColorShirtObject , longSleeveMultiColorShirtObject), false);// => should pass