From c9aeff4bbe2c993292682b28a0323b941f4053c9 Mon Sep 17 00:00:00 2001 From: Alisa Date: Fri, 28 Aug 2020 09:40:52 +0300 Subject: [PATCH 1/2] solution --- src/deepEqual.js | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/deepEqual.js b/src/deepEqual.js index 860208b..c59b61a 100644 --- a/src/deepEqual.js +++ b/src/deepEqual.js @@ -20,7 +20,36 @@ * @return {boolean} */ function deepEqual(a, b) { - // write code here + // Different data types, no need to compare + if (typeof a !== typeof b) { + return false; + } + + // Compare primitives and null + if ((typeof a !== 'object' && typeof b !== 'object') + || (a === null || b === null)) { + return a === b; + } + + // Compare objects + if (typeof a === 'object' && typeof b === 'object') { + if (Object.keys(a).length !== Object.keys(b).length) { + return false; + } + + // Compare objects with same number of keys + for (const key in a) { + if (!b.hasOwnProperty(key)) { + return false; + } + + if (!deepEqual(a[key], b[key])) { + return false; + } + } + + return true; + } } module.exports = deepEqual; From aee8f28bd0fd7e978003d20885da9087d90746a7 Mon Sep 17 00:00:00 2001 From: Alisa Date: Fri, 28 Aug 2020 09:50:40 +0300 Subject: [PATCH 2/2] delete comments --- src/deepEqual.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/deepEqual.js b/src/deepEqual.js index c59b61a..89eeeb4 100644 --- a/src/deepEqual.js +++ b/src/deepEqual.js @@ -20,24 +20,20 @@ * @return {boolean} */ function deepEqual(a, b) { - // Different data types, no need to compare if (typeof a !== typeof b) { return false; } - // Compare primitives and null if ((typeof a !== 'object' && typeof b !== 'object') || (a === null || b === null)) { return a === b; } - // Compare objects if (typeof a === 'object' && typeof b === 'object') { if (Object.keys(a).length !== Object.keys(b).length) { return false; } - // Compare objects with same number of keys for (const key in a) { if (!b.hasOwnProperty(key)) { return false;