Skip to content

Commit 7ab075a

Browse files
authored
Merge pull request #1 from imran120198/imran
added code snippet 34
2 parents e44c51a + db75c25 commit 7ab075a

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,3 +1018,33 @@ Since a and b reference the same array, both console.log(a) and console.log(b) w
10181018
This is different from the previous example where ... spread operator was used, which created a new array with the same values as the original array instead of referencing the same array.
10191019

10201020
</details>
1021+
1022+
<details>
1023+
<summary>
1024+
<h3>34. What is the output of the following code?</h3>
1025+
1026+
```jsx
1027+
var companies = [
1028+
{id: "1", name:"Facebook"},
1029+
{id: "2", name:"Apple"},
1030+
{id: "3", name:"Google"},
1031+
]
1032+
1033+
companies.sort((a,b) => (a.name > b.name ? -1 : 1))
1034+
console.log(companies)
1035+
1036+
```
1037+
1038+
</summary>
1039+
1040+
The output of the code will be:
1041+
1042+
Answer:
1043+
1044+
```bash
1045+
[ {id: "3", name:"Google"} , {id: "1", name:"Facebook"} , {id: "2", name:"Apple"} ]
1046+
```
1047+
1048+
The comparison function takes two parameters, "a" and "b", which represent two elements being compared in the array. If the "name" property of "a" comes before the "name" property of "b" in alphabetical order, then the function returns -1, which means "a" should come before "b" in the sorted array. Otherwise, if the "name" property of "a" comes after the "name" property of "b" in alphabetical order, then the function returns 1, which means "b" should come before "a" in the sorted array.
1049+
1050+
</details>

0 commit comments

Comments
 (0)