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 db1da78

Browse files
committed
new question added
1 parent e6b344a commit db1da78

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

‎README.md

Lines changed: 43 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 July 19, 2023 </span>
7+
<span style=" font-size: 1rem; border-bottom: 1px solid grey;"> Updated Sept 14, 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

@@ -1447,3 +1447,45 @@ After the generator is done, any further calls to `generator.next()` will keep r
14471447
Hence, the correct answer is A: { value: 1, done: false }, { value: 2, done: false }, { value: 3, done: true }.
14481448
14491449
</details>
1450+
1451+
<details>
1452+
<summary>
1453+
<h3>44. Write a function to make the following code snippet work?</h3>
1454+
1455+
```js
1456+
console.log(sum(4, 6, 8, 10).value); //output - 28
1457+
console.log(sum(4)(6)(8)(10).value); //output - 28
1458+
```
1459+
1460+
</summary>
1461+
1462+
Answer
1463+
1464+
```js
1465+
function sum(...args) {
1466+
const ans = args.reduce((a, b) => a + b, 0); //just to get sum of all the array elements
1467+
1468+
const myFunc = (num) => {
1469+
return sum(num, ...args);
1470+
};
1471+
1472+
myFunc.value = ans;
1473+
1474+
return myFunc;
1475+
}
1476+
1477+
console.log(sum(4, 6, 8, 10).value); //output - 28
1478+
console.log(sum(4)(6)(8)(10).value); //output - 28
1479+
```
1480+
1481+
The sum function takes any number of arguments using the rest parameter ...args and calculates the sum of those arguments.
1482+
1483+
It defines a nested function called myFunc, which takes a new number num and returns a new instance of the sum function with the accumulated sum and the new number.
1484+
1485+
The current sum value is assigned to a property named value on myFunc.
1486+
1487+
The myFunc function is then returned, allowing you to chain multiple function calls together.
1488+
1489+
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.
1490+
1491+
</details>

0 commit comments

Comments
(0)

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