Skip to content
Original file line number Diff line number Diff line change
@@ -1,2 +1,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`.
راه‌حل از `count` در متغیر محلی استفاده می‌کند اما متدهای اضافی درست درون `counter` نوشته شده‌اند. آنها محیط لغوی بیرونی یکسان را به اشتراک می‌گذارند و همچنین می‌توانند به `count` کنونی دسترسی پیدا کنند.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ importance: 5

---

# Set and decrease for counter
# تنظیم و کم کردن برای شمارنده

Modify the code of `makeCounter()` so that the counter can also decrease and set the number:
کد `makeCounter()` را طوری تغییر دهید که شمارنده بتواند هم عدد را تنظیم کند و هم آن را کاهش دهد:

- `counter()` should return the next number (as before).
- `counter.set(value)` should set the counter to `value`.
- `counter.decrease()` should decrease the counter by 1.
- `counter()` باید عدد بعدی را برگرداند (مانند قبل).
- `counter.set(value)` باید شمارنده را در `value` تنظیم کند.
- `counter.decrease()` باید از شمارنده به اندازه 1 کم کند.

See the sandbox code for the complete usage example.
برای دیدن مثالی کامل از نحوه استفاده، کد جعبهٔ شنی(sandbox) را ببینید.

P.S. You can use either a closure or the function property to keep the current count. Or write both variants.
پی‌نوشت: شما می‌توانید از کلوژر یا ویژگی تابع برای حفظ کردن شماره کنونی استفاده کنید. یا هر دو نوع را بنویسید.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

1. For the whole thing to work *anyhow*, the result of `sum` must be function.
2. That function must keep in memory the current value between calls.
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.
1. برای اینکه همه چیز به *هر نحوی* کار کند، نتیجه `sum` باید تابع باشد.
2. آن تابع باید بین فراخوانی‌ها مقدار کنونی را در حافظه ذخیره کند.
3. با توجه به تمرین، تابع باید زمانی که با `==` استفاده می‌شود، تبدیل به عدد شود. تابع‌ها شیء هستند پس تبدیل شدن همانطور که در فصل <info:object-toprimitive> گفته شد اتفاق می‌افتد و ما می‌توانیم متد خودمان را برای برگرداندن عدد بسازیم.

Now the code:
حالا می‌رسیم به کد:

```js demo run
function sum(a) {
Expand All @@ -28,28 +28,28 @@ alert( sum(6)(-1)(-2)(-3) ); // 0
alert( sum(0)(1)(2)(3)(4)(5) ); // 15
```

Please note that the `sum` function actually works only once. It returns function `f`.
لطفا در نظر داشته باشید که تابع `sum` فقط یکبار کار می‌کند. تابع `f` را برمی‌گرداند.

Then, on each subsequent call, `f` adds its parameter to the sum `currentSum`, and returns itself.
سپس در هر فراخوانی زیر مجموعه آن، تابع `f` پارامتر خودش را به جمع `currentSum` اضافه می‌کند و خودش را برمی‌گرداند.

**There is no recursion in the last line of `f`.**
**هیچ بازگشتی در آخرین خط `f` وجود ندارد.**

Here is what recursion looks like:
بازگشتی اینگونه بنظر می‌رسد:

```js
function f(b) {
currentSum += b;
return f(); // <-- recursive call
return f(); // <-- فراخوانی بازگشتی
}
```

And in our case, we just return the function, without calling it:
و در این مورد ما، بدون صدا زدن تابع، ما فقط آن را برمی‌گردانیم:

```js
function f(b) {
currentSum += b;
return f; // <-- does not call itself, returns itself
return f; // <-- خودش را صدا نمی‌زد، خودش را برمی‌گرداند
}
```

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.
این `f` در فراخوانی بعدی استفاده می‌شود و دوباره خودش را برمی‌گرداند، هر چند باری که نیاز باشد. سپس زمانی که به عنوان یک عدد یا رشته استفاده می‌شود -- `toString` مقدار `currentSum` را برمی‌گرداند. ما می‌توانستیم برای تبدیل از `Symbol.toPrimitive` یا `valueOf` اینجا استفاده کنیم.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 2

---

# Sum with an arbitrary amount of brackets
# جمع زدن با تعداد دلخواهی از پرانتز

Write function `sum` that would work like this:
تابع `sum` را بنویسید که اینگونه کار کند:

```js
sum(1)(2) == 3; // 1 + 2
Expand All @@ -14,4 +14,4 @@ sum(6)(-1)(-2)(-3) == 0
sum(0)(1)(2)(3)(4)(5) == 15
```

P.S. Hint: you may need to setup custom object to primitive conversion for your function.
پی‌نوشت: راهنمایی: شما ممکن است نیاز داشته باشید که تبدیل شیء به مقدار اصلی سفارشی برای تابع خود بنویسید.
Loading