-
Notifications
You must be signed in to change notification settings - Fork 0
/
eqObjects.js
70 lines (55 loc) · 1.9 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
const eqArrays = function (Array1, Array2) {
//callback function, compares two arrays element by element
let result = true;
for (let x = 0; x < Array1.length; x++) {
Array1[x] === Array2[x] ? (result = true) : (result = false); //if all the elements match, result is true
}
return result; //return result to assertArraysEqual
}
const assertEqual = function(Value1, Value2) {
//eqArrays(Value1, Value2)
//log a msg, based on if the values are the same or not
let msg1 = `${Value1} === ${Value2}`;
let msg2 = `${Value1} !== ${Value2}`;
if (Value1 === Value2) {
console.log(`Assertion passed: ${msg1}`);
}
console.assert(Value1 === Value2, `${msg2}`);
};
const eqObjects = function(object1, object2) {
//if objects are unequal in length we know its false, therefore
if (Object.keys(object1).length !== Object.keys(object2).length) {
return false;
}
//every object key value must be the same, otherwise its false
for (let key in object1) {
//checking if object key exists first
if (!object2.hasOwnProperty(key)) {
return false;
}
if (Array.isArray(object1[key]) && Array.isArray(object2[key])) {
if (!eqArrays(object1[key], object2[key])) {
return false;
}
} else {
//comparing object key values
if (object1[key] !== object2[key]) {
return false;
}
}
}
//objects must be the same at this point, therefore return true
console.log(true);
return true;
};
const ab = { a: "1", b: "2" };
const ba = { b: "2", a: "1" };
//assertEqual(eqObjects(ab, ba), true); // => true
const abc = { a: "1", b: "2", c: "3" };
//assertEqual(eqObjects(ab, abc), false); // => false
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
module.exports = eqObjects;