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 a2c5992

Browse files
Merge pull request #2 from Vasu7389/feature/add_new_question
new question added
2 parents e3f6416 + 06cb6be commit a2c5992

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

β€ŽREADME.md

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: "Prepare for your next 2023 JavaScript interview with these tricky
44
githubPath: "https://github.com/Vasu7389/JavaScript-Interview-Questions-2023"
55
---
66

7-
<span style=" font-size: 1rem; border-bottom: 1px solid grey;"> Updated Sept 14, 2023 </span>
7+
<span style=" font-size: 1rem; border-bottom: 1px solid grey;"> Updated Nov 25, 2023 </span>
88

99
In this article, we will cover a range of JavaScript interview questions, including those related to the latest versions of the language (ES6, ES7, ES8, and ES9).
1010

@@ -1465,3 +1465,59 @@ The myFunc function is then returned, allowing you to chain multiple function ca
14651465
As a result, you can use this sum function to either pass all numbers at once or chain multiple function calls to add numbers incrementally, and it will provide the correct sum when you access the value property.
14661466
14671467
</details>
1468+
1469+
<details>
1470+
<summary>
1471+
<h3>45. Understanding the `.sort()` Method Variations</h3>
1472+
1473+
```js
1474+
const fruits = ["banana", "apple", "orange", "grape", "kiwi"];
1475+
1476+
// Task 1: Sort the array of fruits in alphabetical order (default behavior)
1477+
// Task 2: Sort the array of fruits in descending alphabetical order
1478+
// Task 3: Sort the array of fruits based on the length of the fruit names in ascending order
1479+
// Task 4: Sort the array of fruits in ascending order by the second character of each fruit name
1480+
```
1481+
1482+
</summary>
1483+
1484+
Answer
1485+
1486+
Task 1: Sort in alphabetical order (default behavior)
1487+
1488+
```js
1489+
const alphabeticalOrder = [...fruits].sort();
1490+
console.log(alphabeticalOrder); // Output: ['apple', 'banana', 'grape', 'kiwi', 'orange']
1491+
```
1492+
1493+
Task 2: Sort in descending alphabetical order
1494+
1495+
```js
1496+
const descendingOrder = [...fruits].sort((a, b) => b.localeCompare(a));
1497+
console.log(descendingOrder); // Output: ['orange', 'kiwi', 'grape', 'banana', 'apple']
1498+
```
1499+
1500+
Task 3: Sort based on the length of the fruit names in ascending order
1501+
1502+
```js
1503+
const sortByLength = [...fruits].sort((a, b) => a.length - b.length);
1504+
console.log(sortByLength); // Output: ['kiwi', 'apple', 'grape', 'banana', 'orange']
1505+
```
1506+
1507+
Task 4: Sort in ascending order by the second character of each fruit name
1508+
1509+
```js
1510+
const sortBySecondChar = [...fruits].sort((a, b) => a[1].localeCompare(b[1]));
1511+
console.log(sortBySecondChar); // Output: ['banana', 'kiwi', 'apple', 'orange', 'grape']
1512+
```
1513+
1514+
Explanation:
1515+
1516+
- Task 1 utilizes the default behavior of `.sort()` to arrange elements alphabetically.
1517+
- Task 2 reverses the order by using `localeCompare()` with the reverse comparison.
1518+
- Task 3 sorts the array by the length of the elements, ensuring ascending order.
1519+
- Task 4 specifically compares the second character of each element for sorting.
1520+
1521+
These variations demonstrate different uses of the `.sort()` method by customizing the sorting logic through comparator functions, enabling diverse sorting patterns for arrays.
1522+
1523+
</details>

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /