Can someone walk me through the recursion function in this code?, !deepEqual(a[key], b[key])
. How it compares its values every time it is called?...
function deepEqual(a, b) {
if (a === b) return true;
if (a == null || typeof a != "object" ||
b == null || typeof b != "object") return false;
let keysA = Object.keys(a), keysB = Object.keys(b);
if (keysA.length != keysB.length) return false;
for (let i = 0; i <= keysA.length -1; i++) {
let key = keysA[i];
if (!keysB.includes(key) || !deepEqual(a[key], b[key])) return false;
}
return true;
}
let obj = {here: {is: "an"}, object: 2};
console.log(deepEqual(obj, obj));
// → true
console.log(deepEqual(obj, {here: {is: "as"}, object: 3}));
// → false
console.log(deepEqual(obj, {here: {is: "an"}, object: 2}));
// → true
I really need to understand how the recursion function loop get through the second call without returning true on the first check and false on the second check for
if (a == null || typeof a != "object" ||
b == null || typeof b != "object") return false;
Because the value of !deepEqual(a["is"], b["is"]))
== !deepEqual("is", "is"))
and thus should return true;
on the first check for if (a === b) return true;
. and return false;
on the second check for if (a == null || typeof a != "object" || b == null || typeof b != "object") return false;
because its not an object.
wont that stop the function from completing the check for the remaining properties?
can anyone make this clear enough for me?
let obj = {here: {is: "an", that: "at", name: "ola", here: {is: "as"}, object: 2};
console.log(deepEqual(obj, obj));
// → true
console.log(deepEqual(obj, {here: {is: "an", that: "at", name: "ola", here: {is: "as"}, object: 3}));
// → false
//The only different property value is in the second argument's last property.