closure task
This commit is contained in:
parent
6be43b16a9
commit
c2a9c2da58
4 changed files with 61 additions and 0 deletions
|
@ -0,0 +1,17 @@
|
|||
For the second parentheses to work, the first ones must return a function.
|
||||
|
||||
Like this:
|
||||
|
||||
```js run
|
||||
function sum(a) {
|
||||
|
||||
return function(b) {
|
||||
return a + b; // takes "a" from the outer lexical environment
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
alert( sum(1)(2) ); // 3
|
||||
alert( sum(5)(-1) ); // 4
|
||||
```
|
||||
|
17
1-js/06-advanced-functions/03-closure/6-closure-sum/task.md
Normal file
17
1-js/06-advanced-functions/03-closure/6-closure-sum/task.md
Normal file
|
@ -0,0 +1,17 @@
|
|||
importance: 4
|
||||
|
||||
---
|
||||
|
||||
# Sum with closures
|
||||
|
||||
Write function `sum` that works like this: `sum(a)(b) = a+b`.
|
||||
|
||||
Yes, exactly this way, using double parentheses (not a mistype).
|
||||
|
||||
For instance:
|
||||
|
||||
```js
|
||||
sum(1)(2) = 3
|
||||
sum(5)(-1) = 4
|
||||
```
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue