Skip to content

Commit 6bf6da5

Browse files
committed
new question added
1 parent b6b4fba commit 6bf6da5

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

README.md

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

7-
<span style=" font-size: 1rem; border-bottom: 1px solid grey;"> Updated Jan 02, 2024 </span>
7+
<span style=" font-size: 1rem; border-bottom: 1px solid grey;"> Updated Jan 07, 2024 </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

@@ -1521,3 +1521,33 @@ Explanation:
15211521
These variations demonstrate different uses of the `.sort()` method by customizing the sorting logic through comparator functions, enabling diverse sorting patterns for arrays.
15221522
15231523
</details>
1524+
1525+
<details>
1526+
<summary>
1527+
<h3>46. Explain the output of the following code and correct any errors.</h3>
1528+
1529+
```javascript
1530+
let numbers = [1, 2, 3, 4, 5];
1531+
numbers = numbers.map((number) => number * 2);
1532+
console.log(numbers.reduce((total, num) => total + num));
1533+
```
1534+
1535+
</summary>
1536+
1537+
**Answer:**
1538+
1539+
```javascript
1540+
// Output: 30 (No errors in the code)
1541+
```
1542+
1543+
**Explanation:**
1544+
1545+
1. **Array Creation:** The code starts by creating an array named `numbers` containing the values `[1, 2, 3, 4, 5]`.
1546+
1547+
2. **Array Mapping:** The `map()` method is used to create a new array by applying a function to each element of the original array. In this case, the function `number => number * 2` doubles each number in the array. The new array becomes `[2, 4, 6, 8, 10]`.
1548+
1549+
3. **Array Reduction:** The `reduce()` method is used to reduce the array to a single value by applying a function against an accumulator and each element in the array (from left to right). The function `(total, num) => total + num` adds each number in the array to the `total`, starting with an initial `total` of 0.
1550+
1551+
4. **Output:** The final `console.log()` statement outputs the result of the `reduce()` operation, which is 30 (the sum of all the doubled numbers in the array).
1552+
1553+
</details>

0 commit comments

Comments
 (0)