You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The solution uses`count`in the local variable, but addition methods are written right into the `counter`. They share the same outer lexical environment and also can access the current`count`.
2
+
В рішенні використовується`count`у локальній змінній, але методи додавання записуються прямо в `counter`. Вони поділяють таке ж зовнішнє лексичне середовище, а також можуть отримати доступ до поточного`count`.
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/solution.md
+12-12
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,9 @@
1
1
2
-
1.For the whole thing to work *anyhow*, the result of `sum`must be function.
3
-
2.That function must keep in memory the current value between calls.
4
-
3.According to the task, the function must become the number when used in `==`. Functions are objects, so the conversion happens as described in the chapter <info:object-toprimitive>, and we can provide our own method that returns the number.
2
+
1.Загалом, для того щоб все працювало *хоч як-небудь*, результат `sum`повинен бути функцією.
3
+
2.Ця функція повинна зберігати в пам’яті поточне значення між викликами.
4
+
3.Згідно з завданням, функція повинна стати числом, коли використовується в `==`. Функції -- це об’єкти, тому перетворення відбувається як описано в розділі <info:object-toprimitive>, і ми можемо надати власний метод, який повертає номер.
Please note that the`sum`function actually works only once. It returns function`f`.
31
+
Зверніть увагу, що функція`sum`фактично працює лише раз. Вона повертає функцію`f`.
32
32
33
-
Then, on each subsequent call, `f`adds its parameter to the sum `currentSum`, and returns itself.
33
+
Потім, на кожному наступному виклику, `f`додає свій параметр до суми `currentSum`, і повертає себе.
34
34
35
-
**There is no recursion in the last line of `f`.**
35
+
**В останньому рядку `f` немає ніякої рекурсії.**
36
36
37
-
Here is what recursion looks like:
37
+
Ось як рекурсія виглядає:
38
38
39
39
```js
40
40
functionf(b) {
41
41
currentSum += b;
42
-
returnf(); // <-- recursive call
42
+
returnf(); // <-- рекурсивний виклик
43
43
}
44
44
```
45
45
46
-
And in our case, we just return the function, without calling it:
46
+
А в нашому випадку ми просто повертаємо цю функцію, не викликаючи її:
47
47
48
48
```js
49
49
functionf(b) {
50
50
currentSum += b;
51
-
return f; // <-- does not call itself, returns itself
51
+
return f; // <-- не викликає себе, повертає себе
52
52
}
53
53
```
54
54
55
-
This `f`will be used in the next call, again return itself, as many times as needed. Then, when used as a number or a string -- the`toString`returns the `currentSum`. We could also use `Symbol.toPrimitive`or`valueOf`here for the conversion.
55
+
Ця функція `f`буде використовуватися в наступному виклику і знову поверне себе стільки разів, скільки буде потрібно. Потім, при використанні функції як числа або рядка -- метод`toString`поверне `currentSum`. Тут ми також можемо використовувати `Symbol.toPrimitive`або`valueOf`для конверсії.
0 commit comments