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
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/solution.md
+12-12Lines changed: 12 additions & 12 deletions
Display the source diff
Display the rich diff
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, so 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