Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit db75c25

Browse files
added code snippet 34
1 parent e44c51a commit db75c25

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)

AltStyle によって変換されたページ (->オリジナル) /