-
Notifications
You must be signed in to change notification settings - Fork 0
/
Challenge22.js
37 lines (36 loc) · 1002 Bytes
/
Challenge22.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
**Challenge 22**
Problem Statement: Write a function to deeply compare two objects for equality.
Description: Given two objects, the function should return true if they are deeply equal and false otherwise.
Solution Approach: Recursively compare properties of both objects.
*/
function deepEqual(obj1, obj2)
{
if(typeof obj1 == "object" && typeof obj2 == "object")
{
let keys1 = Object.keys(obj1);
let keys2 = Object.keys(obj2);
if(keys1.length != keys2.length)
{
return false;
}
for(let key of keys1)
{
if(!keys2.includes(key))
{
return false;
}
if(!deepEqual(obj1[key], obj2[key]))
{
return false;
}
}
return true;
}
else
{
return obj1 === obj2;
}
}
console.log(deepEqual({a: 1, b: 2}, {a: 1, b: 2})); // true
console.log(deepEqual({a: 1, b: 2}, {a: 1, b: 3})); // false