Skip to content

Commit 241700b

Browse files
committed
finished translating tasks
1 parent 85403e9 commit 241700b

File tree

7 files changed

+62
-155
lines changed

7 files changed

+62
-155
lines changed
Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11

2-
Let's examine what exactly happens inside `makeArmy`, and the solution will become obvious.
2+
Să examinăm ce se întâmplă mai exact în `makeArmy`, iar soluția va deveni evidentă.
33

4-
1. It creates an empty array `shooters`:
4+
1. Creează o matrice goală `shooters`:
55

66
```js
77
let shooters = [];
88
```
9-
2. Fills it with functions via `shooters.push(function)` in the loop.
9+
2. O umple cu funcții prin `shooters.push(function)` în buclă.
1010

11-
Every element is a function, so the resulting array looks like this:
11+
Fiecare element este o funcție, astfel încât matricea rezultată arată astfel:
1212

1313
```js no-beautify
1414
shooters = [
@@ -25,40 +25,40 @@ Let's examine what exactly happens inside `makeArmy`, and the solution will beco
2525
];
2626
```
2727

28-
3. The array is returned from the function.
28+
3. Matricea este returnată din funcție.
2929

30-
Then, later, the call to any member, e.g. `army[5]()` will get the element `army[5]` from the array (which is a function) and calls it.
30+
Apoi, mai târziu, apelarea oricărui membru, e.g. `army[5]()` va obține elementul `army[5]` din matrice (care este o funcție) și îl apelează.
3131

32-
Now why do all such functions show the same value, `10`?
32+
Acum de ce toate aceste funcții arată aceeași valoare, `10`?
3333

34-
That's because there's no local variable `i` inside `shooter` functions. When such a function is called, it takes `i` from its outer lexical environment.
34+
Asta pentru că nu există o variabilă locală `i` în interiorul funcțiilor `shooter`. Atunci când o astfel de funcție este apelată, aceasta preia `i` din mediul său lexical exterior.
3535

36-
Then, what will be the value of `i`?
36+
Atunci, care va fi valoarea lui `i`?
3737

38-
If we look at the source:
38+
Dacă ne uităm la sursă:
3939

4040
```js
4141
function makeArmy() {
4242
...
4343
let i = 0;
4444
while (i < 10) {
45-
let shooter = function() { // shooter function
46-
alert( i ); // should show its number
45+
let shooter = function() { // funcția shooter
46+
alert( i ); // ar trebui să arate numărul său
4747
};
48-
shooters.push(shooter); // add function to the array
48+
shooters.push(shooter); // adaugă funcția la matrice
4949
i++;
5050
}
5151
...
5252
}
5353
```
5454

55-
We can see that all `shooter` functions are created in the lexical environment of `makeArmy()` function. But when `army[5]()` is called, `makeArmy` has already finished its job, and the final value of `i` is `10` (`while` stops at `i=10`).
55+
Putem vedea că toate funcțiile `shooter` sunt create în mediul lexical al funcției `makeArmy()`. Dar când este apelată `army[5]()`, `makeArmy` și-a terminat deja treaba, iar valoarea finală a lui `i` este `10` (`while` se oprește la `i=10`).
5656

57-
As the result, all `shooter` functions get the same value from the outer lexical environment and that is, the last value, `i=10`.
57+
Ca și rezultat, toate funcțiile `shooter` obțin aceeași valoare din mediul lexical extern și anume, ultima valoare, `i=10`.
5858

5959
![](lexenv-makearmy-empty.svg)
6060

61-
As you can see above, on each iteration of a `while {...}` block, a new lexical environment is created. So, to fix this, we can copy the value of `i` into a variable within the `while {...}` block, like this:
61+
După cum puteți vedea mai sus, la fiecare iterație a unui bloc `while {...}`, un nou mediu lexical este creat. Așadar, pentru a remedia acest lucru, putem copia valoarea lui `i` într-o variabilă în cadrul blocului `while {...}`, astfel:
6262

6363
```js run
6464
function makeArmy() {
@@ -69,8 +69,8 @@ Let's examine what exactly happens inside `makeArmy`, and the solution will beco
6969
*!*
7070
let j = i;
7171
*/!*
72-
let shooter = function() { // shooter function
73-
alert( *!*j*/!* ); // should show its number
72+
let shooter = function() { // funcția shooter
73+
alert( *!*j*/!* ); // ar trebui să arate numărul său
7474
};
7575
shooters.push(shooter);
7676
i++;
@@ -81,18 +81,18 @@ Let's examine what exactly happens inside `makeArmy`, and the solution will beco
8181
8282
let army = makeArmy();
8383
84-
// Now the code works correctly
84+
// Acum codul funcționează corect
8585
army[0](); // 0
8686
army[5](); // 5
8787
```
8888

89-
Here `let j = i` declares an "iteration-local" variable `j` and copies `i` into it. Primitives are copied "by value", so we actually get an independent copy of `i`, belonging to the current loop iteration.
89+
Aici `let j = i` declară o variabilă `j` "de iterație locală" și copiază `i` în ea. Primitivele sunt copiate "după valoare", astfel încât obținem de fapt o copie independentă a lui `i`, aparținând iterației curente a buclei.
9090

91-
The shooters work correctly, because the value of `i` now lives a little bit closer. Not in `makeArmy()` Lexical Environment, but in the Lexical Environment that corresponds to the current loop iteration:
91+
Shooters funcționează corect, deoarece valoarea lui `i` trăiește acum un pic mai aproape. Nu în mediul lexical `makeArmy()`, ci în Mediul Lexical care corespunde iterației buclei curente:
9292

9393
![](lexenv-makearmy-while-fixed.svg)
9494

95-
Such a problem could also be avoided if we used `for` in the beginning, like this:
95+
O astfel de problemă ar putea fi evitată și dacă am folosi `for` la început, astfel:
9696

9797
```js run demo
9898
function makeArmy() {
@@ -102,8 +102,8 @@ Let's examine what exactly happens inside `makeArmy`, and the solution will beco
102102
*!*
103103
for(let i = 0; i < 10; i++) {
104104
*/!*
105-
let shooter = function() { // shooter function
106-
alert( i ); // should show its number
105+
let shooter = function() { // funcția shooter
106+
alert( i ); // ar trebui să arate numărul său
107107
};
108108
shooters.push(shooter);
109109
}
@@ -117,13 +117,13 @@ Let's examine what exactly happens inside `makeArmy`, and the solution will beco
117117
army[5](); // 5
118118
```
119119

120-
That's essentially the same, because `for` on each iteration generates a new lexical environment, with its own variable `i`. So `shooter` generated in every iteration references its own `i`, from that very iteration.
120+
În esență, este același lucru, deoarece `for` generează la fiecare iterație un nou mediu lexical, cu propria sa variabilă `i`. Astfel, `shooter` generat în fiecare iterație face referire la propriul `i`, chiar din acea iterație.
121121

122122
![](lexenv-makearmy-for-fixed.svg)
123123

124-
Now, as you've put so much effort into reading this, and the final recipe is so simple - just use `for`, you may wonder -- was it worth that?
124+
Acum, având în vedere că ați depus atât de mult efort pentru a citi acest lucru, iar rețeta finală este atât de simplă - folosiți doar `for`, vă puteți întreba -- s-a meritat?
125125

126-
Well, if you could easily answer the question, you wouldn't read the solution. So, hopefully this task must have helped you to understand things a bit better.
126+
Ei bine, dacă ați putea răspunde cu ușurință la această întrebare, nu ați citi soluția. Așadar, sperăm că această sarcină să vă fi ajutat să înțelegeți un pic mai bine lucrurile.
127127

128-
Besides, there are indeed cases when one prefers `while` to `for`, and other scenarios, where such problems are real.
128+
În rest, există într-adevăr cazuri în care se preferă `while` în locul lui `for`, precum și alte scenarii, în care astfel de probleme sunt reale.S
129129

1-js/06-advanced-functions/03-closure/10-make-army/task.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,40 @@ importance: 5
22

33
---
44

5-
# Army of functions
5+
# Armata de funcții
66

7-
The following code creates an array of `shooters`.
7+
Următorul cod creează o matrice de `shooters`.
88

9-
Every function is meant to output its number. But something is wrong...
9+
Fiecare funcție este menită să emită numărul său. Dar ceva nu este în regulă...
1010

1111
```js run
1212
function makeArmy() {
1313
let shooters = [];
1414

1515
let i = 0;
1616
while (i < 10) {
17-
let shooter = function() { // create a shooter function,
18-
alert( i ); // that should show its number
17+
let shooter = function() { // crează o funcție shooter,
18+
alert( i ); // care ar trebui să arate numărul său
1919
};
20-
shooters.push(shooter); // and add it to the array
20+
shooters.push(shooter); // și adăugați-l la matrice
2121
i++;
2222
}
2323

24-
// ...and return the array of shooters
24+
// ...și returnează matricea de shooters
2525
return shooters;
2626
}
2727

2828
let army = makeArmy();
2929

3030
*!*
31-
// all shooters show 10 instead of their numbers 0, 1, 2, 3...
32-
army[0](); // 10 from the shooter number 0
33-
army[1](); // 10 from the shooter number 1
34-
army[2](); // 10 ...and so on.
31+
// toți shooters arată 10 în loc de numerele lor 0, 1, 2, 3...
32+
army[0](); // 10 de la shooter cu numărul 0
33+
army[1](); // 10 de la shooter cu numărul 1
34+
army[2](); // 10 ...și așa mai departe.
3535
*/!*
3636
```
3737

38-
Why do all of the shooters show the same value?
38+
De ce toți shooters arată aceeași valoare?
3939

40-
Fix the code so that they work as intended.
40+
Remediați codul astfel încât acestea să funcționeze așa cum a fost intenționat.
4141

1-js/06-advanced-functions/03-closure/8-filter-through-function/_js.view/source.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
let arr = [1, 2, 3, 4, 5, 6, 7];
33

44
function inBetween(a, b) {
5-
// ...your code...
5+
// ...codul tău...
66
}
77

88
function inArray(arr) {
9-
// ...your code...
9+
// ...codul tău...
1010
}

1-js/06-advanced-functions/03-closure/8-filter-through-function/task.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@ importance: 5
22

33
---
44

5-
# Filter through function
5+
# Filtrează printr-o funcție
66

7-
We have a built-in method `arr.filter(f)` for arrays. It filters all elements through the function `f`. If it returns `true`, then that element is returned in the resulting array.
7+
Avem o metodă încorporată `arr.filter(f)` pentru matrici. Aceasta filtrează toate elementele prin intermediul funcției `f`. Dacă returnează `true`, atunci elementul respectiv este returnat în matricea rezultată.
88

9-
Make a set of "ready to use" filters:
9+
Creați un set de filtre "gata de utilizare":
1010

11-
- `inBetween(a, b)` -- between `a` and `b` or equal to them (inclusively).
12-
- `inArray([...])` -- in the given array.
11+
- `inBetween(a, b)` -- între `a` și `b` sau egal cu ele (inclusiv).
12+
- `inArray([...])` -- în matricea dată.
1313

14-
The usage must be like this:
14+
Modul de utilizare trebuie să fie următorul:
1515

16-
- `arr.filter(inBetween(3,6))` -- selects only values between 3 and 6.
17-
- `arr.filter(inArray([1,2,3]))` -- selects only elements matching with one of the members of `[1,2,3]`.
16+
- `arr.filter(inBetween(3,6))` -- selectează numai valorile cuprinse între 3 și 6.
17+
- `arr.filter(inArray([1,2,3]))` -- selectează numai elementele care se potrivesc cu unul dintre membrii din `[1,2,3]`.
1818

19-
For instance:
19+
De exemplu:
2020

2121
```js
22-
/* .. your code for inBetween and inArray */
22+
/* ... codul dvs. pentru inBetween și inArray */
2323
let arr = [1, 2, 3, 4, 5, 6, 7];
2424

2525
alert( arr.filter(inBetween(3, 6)) ); // 3,4,5,6

0 commit comments

Comments
 (0)