Skip to content

Commit 41ded27

Browse files
authored
Function object, NFE (#198)
1 parent 1a64084 commit 41ded27

File tree

5 files changed

+121
-121
lines changed

5 files changed

+121
-121
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11

2-
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`.

1-js/06-advanced-functions/06-function-object/2-counter-inc-dec/task.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ importance: 5
22

33
---
44

5-
# Set and decrease for counter
5+
# Встановити і зменшити лічильник
66

7-
Modify the code of `makeCounter()` so that the counter can also decrease and set the number:
7+
Змініть код `makeCounter()` так, щоб лічильник міг також зменшити та встановити рахунок:
88

9-
- `counter()` should return the next number (as before).
10-
- `counter.set(value)` should set the counter to `value`.
11-
- `counter.decrease()` should decrease the counter by 1.
9+
- `counter()` повинен повернути наступний рахунок (як раніше).
10+
- `counter.set(value)` повинен встановити лічильник в значення `value`.
11+
- `counter.decrease()` повинен зменшити лічильник на 1.
1212

13-
See the sandbox code for the complete usage example.
13+
Див. код пісочниці з повним прикладом використання.
1414

15-
P.S. You can use either a closure or the function property to keep the current count. Or write both variants.
15+
P.S. Ви можете використовувати або замикання, або властивість функції, щоб зберегти поточний рахунок. Або напишіть обидва варіанти.

1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/solution.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

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>, і ми можемо надати власний метод, який повертає номер.
55

6-
Now the code:
6+
Тепер код:
77

88
```js demo run
99
function sum(a) {
@@ -28,28 +28,28 @@ alert( sum(6)(-1)(-2)(-3) ); // 0
2828
alert( sum(0)(1)(2)(3)(4)(5) ); // 15
2929
```
3030

31-
Please note that the `sum` function actually works only once. It returns function `f`.
31+
Зверніть увагу, що функція `sum` фактично працює лише раз. Вона повертає функцію `f`.
3232

33-
Then, on each subsequent call, `f` adds its parameter to the sum `currentSum`, and returns itself.
33+
Потім, на кожному наступному виклику, `f` додає свій параметр до суми `currentSum`, і повертає себе.
3434

35-
**There is no recursion in the last line of `f`.**
35+
**В останньому рядку `f` немає ніякої рекурсії.**
3636

37-
Here is what recursion looks like:
37+
Ось як рекурсія виглядає:
3838

3939
```js
4040
function f(b) {
4141
currentSum += b;
42-
return f(); // <-- recursive call
42+
return f(); // <-- рекурсивний виклик
4343
}
4444
```
4545

46-
And in our case, we just return the function, without calling it:
46+
А в нашому випадку ми просто повертаємо цю функцію, не викликаючи її:
4747

4848
```js
4949
function f(b) {
5050
currentSum += b;
51-
return f; // <-- does not call itself, returns itself
51+
return f; // <-- не викликає себе, повертає себе
5252
}
5353
```
5454

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` для конверсії.

1-js/06-advanced-functions/06-function-object/5-sum-many-brackets/task.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ importance: 2
22

33
---
44

5-
# Sum with an arbitrary amount of brackets
5+
# Сума з довільною кількістю дужок
66

7-
Write function `sum` that would work like this:
7+
Напишіть функцію `sum`, яка б працювала так:
88

99
```js
1010
sum(1)(2) == 3; // 1 + 2
@@ -14,4 +14,4 @@ sum(6)(-1)(-2)(-3) == 0
1414
sum(0)(1)(2)(3)(4)(5) == 15
1515
```
1616

17-
P.S. Hint: you may need to setup custom object to primitive conversion for your function.
17+
P.S. Підказка: вам може знадобитися налаштувати кастомний об'єкт, щоб конвертувати примітиви для вашої функції.

0 commit comments

Comments
 (0)