Last active
April 12, 2020 01:53
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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