closure task

This commit is contained in:
Ilya Kantor 2019-12-29 00:33:46 +03:00
parent 6be43b16a9
commit c2a9c2da58
4 changed files with 61 additions and 0 deletions

View file

@ -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
```

View 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
```