Skip to content

Instantly share code, notes, and snippets.

@yitonghe00
Last active April 12, 2020 01:53
Show Gist options
  • Save yitonghe00/e058ca4924f28063c4d7a15ed38087b0 to your computer and use it in GitHub Desktop.
Save yitonghe00/e058ca4924f28063c4d7a15ed38087b0 to your computer and use it in GitHub Desktop.
Excises of Eloquent JavaScript 4th edition. Chapter 04 Problem 04. https://eloquentjavascript.net/04_data.html#i_IJBU+aXOIC
// Your code here.
const deepEqual = (a, b) => {
if (typeof a != 'object' || typeof b != 'object') return a === b;
if (a == null || b == null) return a === b;
const aKeys = Object.keys(a);
const bKeys = Object.keys(b);
if (aKeys.length != bKeys.length) return false;
for (let i = 0 ; i < aKeys.length; i++) {
if (!deepEqual(a[aKeys[i]], b[aKeys[i]])) return false;
}
return true;
}
let obj = {here: {is: "an"}, object: 2};
console.log(deepEqual(obj, obj));
// → true
console.log(deepEqual(obj, {here: 1, object: 2}));
// → false
console.log(deepEqual(obj, {here: {is: "an"}, object: 2}));
// → true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment