You are on page 1of 1

/**

* @param {any} o1
* @param {any} o2
* @return {boolean}
*/
var areDeeplyEqual = function(o1, o2) {
// CASE 4
// primitive: number, string, boolean, undefined
if (typeof o1 !== 'object') {
return o1 === o2;
}
// EXTERNAL VALIDATION
if (Object.keys(o1).length !== Object.keys(o2).length) {
return false
}

let o1_keys = JSON.stringify(Object.keys(o1).sort())


let o2_keys = JSON.stringify(Object.keys(o2).sort())
let o1_values = Object.values(o1)
let o2_values = Object.values(o2)

let booVal = true;


for(let i = 0; i < o1_keys.length; i++) {
if(o1_values[i] !== o2_values[i]) {
booVal = false;
}
}

// CASE 1
if(o1_keys === o2_keys && booVal) {
return "The keys and values match exactly.";
};
// CASE 3
for (let key of o1_keys) {
if (!areDeeplyEqual(o1[key], o2[key])) {
return false;
}
}
// CASE 2
if(o1_keys === o2_keys) {
return "Although the keys are in a different order, they still match
exactly."
};
};

You might also like