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/01-recursion/04-output-single-linked-list/solution.md
+13-13Lines changed: 13 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
-
# Loop-based solution
1
+
# Soluție bazată pe loop
2
2
3
-
The loop-based variant of the solution:
3
+
Varianta soluției bazată pe loop:
4
4
5
5
```js run
6
6
let list = {
@@ -30,7 +30,7 @@ function printList(list) {
30
30
printList(list);
31
31
```
32
32
33
-
Please note that we use a temporary variable `tmp`to walk over the list. Technically, we could use a function parameter `list` instead:
33
+
Vă rugăm să rețineți că folosim o variabilă temporară `tmp`pentru a parcurge lista. Din punct de vedere tehnic, am putea folosi în schimb un parametru de funcție `list`:
34
34
35
35
```js
36
36
functionprintList(list) {
@@ -43,15 +43,15 @@ function printList(list) {
43
43
}
44
44
```
45
45
46
-
...But that would be unwise. In the future we may need to extend a function, do something else with the list. If we change `list`, then we lose such ability.
46
+
...Dar asta ar fi neînțelept. În viitor s-ar putea să avem nevoie să extindem o funcție, să facem altceva cu lista. Dacă schimbăm `list`, atunci pierdem o astfel de abilitate.
47
47
48
-
Talking about good variable names, `list`here is the list itself. The first element of it. And it should remain like that. That's clear and reliable.
48
+
Vorbind despre nume de variabile bune, `list`aici este lista însăși. Primul element al acesteia. Și ar trebui să rămână așa. Este clar și de încredere.
49
49
50
-
From the other side, the role of `tmp`is exclusively a list traversal, like `i`in the`for` loop.
50
+
De cealaltă parte, rolul lui `tmp`este exclusiv o traversare a listei, ca și `i`în bucla`for`.
51
51
52
-
# Recursive solution
52
+
# Soluție recursivă
53
53
54
-
The recursive variant of `printList(list)`follows a simple logic: to output a list we should output the current element `list`, then do the same for`list.next`:
54
+
Varianta recursivă a `printList(list)`urmează o logică simplă: pentru a scoate o listă trebuie să scoatem elementul curent `list`, apoi să facem același lucru pentru`list.next`:
55
55
56
56
```js run
57
57
let list = {
@@ -70,19 +70,19 @@ let list = {
70
70
71
71
functionprintList(list) {
72
72
73
-
alert(list.value); //output the current item
73
+
alert(list.value); //afișează elementul curent
74
74
75
75
if (list.next) {
76
-
printList(list.next); //do the same for the rest of the list
76
+
printList(list.next); //procedează la fel pentru restul listei
77
77
}
78
78
79
79
}
80
80
81
81
printList(list);
82
82
```
83
83
84
-
Now what's better?
84
+
Acum ce este mai bine?
85
85
86
-
Technically, the loop is more effective. These two variants do the same, but the loop does not spend resources for nested function calls.
86
+
Din punct de vedere tehnic, un loop este mai eficient. Aceste două variante fac același lucru, dar loop-ul nu consumă resurse pentru apeluri de funcții nested.
87
87
88
-
From the other side, the recursive variant is shorter and sometimes easier to understand.
88
+
De partea cealaltă, varianta recursivă este mai scurtă și uneori mai ușor de înțeles.
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/01-recursion/05-output-single-linked-list-reverse/solution.md
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
-
# Using a recursion
1
+
# Folosind o recursiune
2
2
3
-
The recursive logic is a little bit tricky here.
3
+
Logica recursivă este un pic mai înșelătore aici.
4
4
5
-
We need to first output the rest of the list and *then* output the current one:
5
+
Trebuie să scoatem mai întâi restul listei și *apoi* să o scoatem pe cea curentă:
6
6
7
7
```js run
8
8
let list = {
@@ -31,13 +31,13 @@ function printReverseList(list) {
31
31
printReverseList(list);
32
32
```
33
33
34
-
# Using a loop
34
+
# Utilizarea unui loop
35
35
36
-
The loop variant is also a little bit more complicated than the direct output.
36
+
Varianta loop este de asemenea un pic mai complicată decât scoaterea directă.
37
37
38
-
There is no way to get the last value in our `list`. We also can't "go back".
38
+
Nu există nicio modalitate de a obține ultima valoare din `list`. De asemenea nu putem "merge înapoi".
39
39
40
-
So what we can do is to first go through the items in the direct order and remember them in an array, and then output what we remembered in the reverse order:
40
+
Așadar, ceea ce putem face este să parcurgem mai întâi elementele în ordine directă și să le reținem într-un array, iar apoi să scoatem ceea ce am reținut în ordine inversă:
41
41
42
42
```js run
43
43
let list = {
@@ -71,4 +71,4 @@ function printReverseList(list) {
71
71
printReverseList(list);
72
72
```
73
73
74
-
Please note that the recursive solution actually does exactly the same: it follows the list, remembers the items in the chain of nested calls (in the execution context stack), and then outputs them.
74
+
Vă rugăm să notați că soluția recursivă face de fapt exact la fel: urmărește lista, își amintește elementele din lanțul de apeluri nested (în execution context stack) și apoi le scoate.
0 commit comments