Implement a function deepEqual in Javascript that takes two arguments and returns true if the arguments are deep equal (as defined below) false if the arguments are not deep equal. This function must handle values of the following types: Numbers Boolean values Strings null values undefined values Objects, including arrays Definition of Deep Equality Case 1. If two values are primitive and are strictly equal, then they are deep equal. Case 2: If two values are primitive and are not strictly equal, then they are not deep equal. Case 3. If one value is an object and the other value is primitive, then they are not deep equal. Case 4: If two values are both objects and are strictly equal, then they are deep equal. Case 5: If two values are both objects and are not strictly equal, then we determine deep equality as follows: 5.1 If both values are non-array objects, then they are deep equal if both the objects have exactly the same properties and the values of these properties are deep equal. The order of properties in these non-array objects doesn't matter for deep equality. 5.2 If both values are arrays, then they are deep equal if their elements are deep equal and these elements are in the same order. 5.3 All other pairs of objects are not deep equal.
Implement a function deepEqual in Javascript that takes two arguments and returns
- true if the arguments are deep equal (as defined below)
- false if the arguments are not deep equal.
This function must handle values of the following types:
-
- Numbers
- Boolean values
- Strings
- null values
- undefined values
- Objects, including arrays
Definition of Deep Equality
Case 1. If two values are primitive and are strictly equal, then they are deep equal.
Case 2: If two values are primitive and are not strictly equal, then they are not deep equal.
Case 3. If one value is an object and the other value is primitive, then they are not deep equal.
Case 4: If two values are both objects and are strictly equal, then they are deep equal.
Case 5: If two values are both objects and are not strictly equal, then we determine deep equality as follows:
5.1 If both values are non-array objects, then they are deep equal if both the objects have exactly the same properties and the values of these properties are deep equal. The order of properties in these non-array objects doesn't matter for deep equality.
5.2 If both values are arrays, then they are deep equal if their elements are deep equal and these elements are in the same order.
5.3 All other pairs of objects are not deep equal.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images