-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheqObjects.js
72 lines (56 loc) · 2.22 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
const assertEqual = function(actual, expected) {
if (actual !== expected)
return console.log(`🛑🛑🛑 Assertion Failed: ${actual} !== ${expected}`);
if (actual === expected)
return console.log(`✅✅✅ Assertion Passed: ${actual} === ${expected}`);
};
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 eqObjects = function(object1, object2) {
let objKeys1 = Object.keys(object1);
let objKeys2 = Object.keys(object2);
if (objKeys1.length !== objKeys2.length)
return false;
for (let i = 0; i < objKeys1.length; i++) {
let key = objKeys1[i];
if (Array.isArray(object1[key]) && Array.isArray(object2[key])) {
// If the values are arrays, use eqArrays to compare them
if (eqArrays(object1[key], object2[key]) === false) {
return false;
}
} else {
// If the values are not arrays, compare them directly
if (object1[key] !== object2[key]) {
return false;
}
}
}
return true;
};
module.exports = eqObjects;
// //Test Conditions
const shirtObject = { color: "red", size: "medium" };
const anotherShirtObject = { size: "medium", color: "red" };
eqObjects(shirtObject, anotherShirtObject); // => true
//We need to use that return value in combination with assertEquals to test if the function is working correctly.
assertEqual(eqObjects(shirtObject, anotherShirtObject), true);
const longSleeveShirtObject = { size: "medium", color: "red", sleeveLength: "long" };
eqObjects(shirtObject, longSleeveShirtObject); // => false
assertEqual(eqObjects(shirtObject, longSleeveShirtObject), false);
//Test Conditions
const multiColorShirtObject = { colors: ["red", "blue"], size: "medium" };
const anotherMultiColorShirtObject = { size: "medium", colors: ["red", "blue"] };
let result1 = eqObjects(multiColorShirtObject , anotherMultiColorShirtObject); // => true
const longSleeveMultiColorShirtObject = { size: "medium", colors: ["red", "blue"], sleeveLength: "long" };
let result2 = eqObjects(multiColorShirtObject , longSleeveMultiColorShirtObject);
console.log(result1);
console.log(result2);