You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+10-10Lines changed: 10 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -564,39 +564,39 @@ Note that we chain both .replace() methods in succession such that both cases ar
564
564
565
565
<b>7. Deep Comparison </b>
566
566
567
-
Comparing arrays can be troublesome, not to mention multi-dimensional arrays. Here is something simple to help.
567
+
Comparing objects can be troublesome, not to mention multi-dimensional objects/arrays. Here is something simple to help.
568
568
569
-
__The challenge:__ <p> - JS Objects, contrary to the way we perceive it, are simply pointers to the data stored, rather than the actual data itself. Thus, to compare objects/arrays a and b we cannot just use normal comparison operators.</p>
569
+
__The challenge:__ <p> - JS Objects, contrary to the way we perceive it, are simply pointers to the data stored, rather than the actual data itself. Thus, to compare objects/arrays ```a``` and ```b``` we cannot just use normal comparison operators.</p>
570
570
```js
571
571
a===b//false
572
572
```
573
573
<p> - Use of multidimensional objects/arrays is possible, making it difficult to compare simply by iteration since we don't know the depth of a value. </p>
574
574
575
-
<p> - Different data types like objects that also cannot be compared directly must also be taken into consideration. <p>
575
+
<p> - Different data types like Dates and ```undefined``` must also be taken into consideration. <p>
576
576
577
-
<p>Given the above, return a boolean signifying whether arrays a and b are equivalent in content. </p>
577
+
<p>Given the above, return a boolean signifying whether ```a``` and ```b``` are equivalent in content. </p>
578
578
579
579
__Algorithmic Thinking:__ <p>As we would be comparing each item contained in the objects, a loop may be the first instinct to solving it. However, with the potential of multidimensional iterables, we would have to disect nested arrays in the same way when we encounter them. A combination of iteration and recursion is therefore necessary. So for each item of the array a data type check is necessary as well, to allow execution of a relevant comparison.
580
580
581
581
Breaking it down:
582
-
* check if a === b
583
-
* check if a and b are both iterable
584
-
* iterate over a using keys and call deepCompare recursively
582
+
* check if ```a===b```
583
+
* check if ```a``` and ```b``` are both iterable
584
+
* iterate over ```a``` using keys and call deepCompare recursively
585
585
</p>
586
586
587
587
588
588
__code Implementation:__ <p>
589
-
Firstly, we'll do the most simple check of a === b to avoid unnecessary complexity. This will process all of the equal literal values for us.
589
+
Firstly, we'll do the most simple check of ```a===b``` to avoid unnecessary complexity. This will process all of the equal literal values for us.
590
590
591
591
```js
592
592
if(a === b) returntrue
593
593
```
594
594
595
-
Then comes the interesting part! There are several data types we need to look out for: Objects, Arrays(which JS treats as an object), and Dates(which is also treated as an object!), thus all we have to do is check if both a and is type object. If not, we can just return false as they didn't pass th a === b test.
595
+
Then comes the interesting part! There are several data types we need to look out for: Objects, Arrays(which JS treats as an object), and Dates(which is also treated as an object!), thus all we have to do is check if both a and is type object. If not, we can just return false as they didn't pass the ```a===b``` test.
596
596
597
597
Next, we can process the dates first, as that doesn't require iteration. Make sure to compare ```Date.valueOf()``` instead.
598
598
599
-
Lastly, by taking the keys of the iterables we can compare the length of a and b, then make use of built-in Array.some method to check if any values of the two iterables don't match.
599
+
Lastly, by taking the keys of the iterables we can compare the length of ```a``` and ```b```, then make use of built-in Array.some method to check if any values of the two iterables don't match.
0 commit comments