Skip to content

Commit a0a8bd6

Browse files
committed
translated entire article
Hopefully it will be right first time, which rarely happens. But I'm confident. 1-js/02-first-steps/10-ifelse/article.md
1 parent 3d169ac commit a0a8bd6

File tree

1 file changed

+85
-85
lines changed

1 file changed

+85
-85
lines changed
Lines changed: 85 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,117 @@
1-
# Conditional branching: if, '?'
1+
# Ramificare condiționată: if, '?'
22

3-
Sometimes, we need to perform different actions based on different conditions.
3+
Uneori, trebuie să efectuăm acțiuni diferite în funcție de condiții diferite.
44

5-
To do that, we can use the `if` statement and the conditional operator `?`, that's also called a "question mark" operator.
5+
Pentru a face asta, putem folosi acest `if` și operatorul condițional `?`, care se mai numește și operatorul "semn de întrebare".
66

7-
## The "if" statement
7+
## Instrucțiunea "if"
88

9-
The `if(...)` statement evaluates a condition in parentheses and, if the result is `true`, executes a block of code.
9+
Instrucțiunea `if(...)` evaluează o condiție între paranteze și, dacă rezultatul este `true`, execută un bloc de cod.
1010

11-
For example:
11+
De exemplu:
1212

1313
```js run
14-
let year = prompt('In which year was ECMAScript-2015 specification published?', '');
14+
let year = prompt('În ce an a fost publicată specificația ECMAScript-2015?', '');
1515

1616
*!*
17-
if (year == 2015) alert( 'You are right!' );
17+
if (year == 2015) alert( 'Ai dreptate!' );
1818
*/!*
1919
```
2020

21-
In the example above, the condition is a simple equality check (`year == 2015`), but it can be much more complex.
21+
În exemplul de mai sus, condiția este o simplă verificare a egalității (`year == 2015`), dar poate fi mult mai complexă.
2222

23-
If we want to execute more than one statement, we have to wrap our code block inside curly braces:
23+
Dacă vrem să executăm mai mult de o instrucțiune, trebuie să înfășurăm blocul nostru de cod în interiorul unor acolade:
2424

2525
```js
2626
if (year == 2015) {
27-
alert( "That's correct!" );
28-
alert( "You're so smart!" );
27+
alert( "Este corect!" );
28+
alert( "Ești atât de deștept!" );
2929
}
3030
```
3131

32-
We recommend wrapping your code block with curly braces `{}` every time you use an `if` statement, even if there is only one statement to execute. Doing so improves readability.
32+
Vă recomandăm să vă înfășurați blocul de cod cu acolade `{}` de fiecare dată când folosiți o instrucțiune `if`, chiar dacă există doar o singură instrucțiune de executat. Procedând astfel se îmbunătățește lizibilitatea.
3333

34-
## Boolean conversion
34+
## Conversia boolean
3535

36-
The `if ()` statement evaluates the expression in its parentheses and converts the result to a boolean.
36+
Instrucțiunea `if (...)` evaluează expresia din paranteze și convertește rezultatul în boolean.
3737

38-
Let's recall the conversion rules from the chapter <info:type-conversions>:
38+
Să ne reamintim regulile de conversie din capitolul <info:type-conversions>:
3939

40-
- A number `0`, an empty string `""`, `null`, `undefined`, and `NaN` all become `false`. Because of that they are called "falsy" values.
41-
- Other values become `true`, so they are called "truthy".
40+
- Un număr `0`, un string gol `""`, `null`, `undefined` și `NaN` devin toate `false`. Din acest motiv, ele sunt numite valori "falsy".
41+
- Alte valori devin `true`, de aceea se numesc "truthy".
4242

43-
So, the code under this condition would never execute:
43+
Așadar, codul din această condiție nu se va executa niciodată:
4444

4545
```js
46-
if (0) { // 0 is falsy
46+
if (0) { // 0 este falsy
4747
...
4848
}
4949
```
5050

51-
...and inside this condition -- it always will:
51+
...și în interiorul acestei condiții -- întotdeauna va fi așa:
5252

5353
```js
54-
if (1) { // 1 is truthy
54+
if (1) { // 1 este truthy
5555
...
5656
}
5757
```
5858

59-
We can also pass a pre-evaluated boolean value to `if`, like this:
59+
Putem de asemenea să transmitem o valoare boolean pre-evaluată la `if`, astfel:
6060

6161
```js
62-
let cond = (year == 2015); // equality evaluates to true or false
62+
let cond = (year == 2015); // egalitatea se evaluează la true sau false
6363

6464
if (cond) {
6565
...
6666
}
6767
```
6868

69-
## The "else" clause
69+
## Clauza "else"
7070

71-
The `if` statement may contain an optional "else" block. It executes when the condition is falsy.
71+
Instrucțiunea `if` poate conține un bloc opțional "else". Acesta se execută atunci când condiția este falsy.
7272

73-
For example:
73+
De exemplu:
7474
```js run
75-
let year = prompt('In which year was the ECMAScript-2015 specification published?', '');
75+
let year = prompt('În ce an a fost publicată specificația ECMAScript-2015?', '');
7676

7777
if (year == 2015) {
7878
alert( 'You guessed it right!' );
7979
} else {
80-
alert( 'How can you be so wrong?' ); // any value except 2015
80+
alert( 'How can you be so wrong?' ); // orice valoare cu excepția 2015
8181
}
8282
```
8383

84-
## Several conditions: "else if"
84+
## Mai multe condiții: "else if"
8585

86-
Sometimes, we'd like to test several variants of a condition. The `else if` clause lets us do that.
86+
Uneori, ne-ar place să testăm mai multe variante ale unei condiții. Clauza `else if` ne permite să facem asta.
8787

88-
For example:
88+
De exemplu:
8989

9090
```js run
91-
let year = prompt('In which year was the ECMAScript-2015 specification published?', '');
91+
let year = prompt('În ce an a fost publicată specificația ECMAScript-2015?', '');
9292

9393
if (year < 2015) {
94-
alert( 'Too early...' );
94+
alert( 'Prea devreme...' );
9595
} else if (year > 2015) {
96-
alert( 'Too late' );
96+
alert( 'Prea târziu' );
9797
} else {
98-
alert( 'Exactly!' );
98+
alert( 'Exact!' );
9999
}
100100
```
101101

102-
In the code above, JavaScript first checks `year < 2015`. If that is falsy, it goes to the next condition `year > 2015`. If that is also falsy, it shows the last `alert`.
102+
În codul de mai sus, JavaScript verifică mai întâi `year < 2015`. Dacă este falsy, trece la următoarea condiție `year > 2015`. Dacă și aceasta este falsy, afișează ultimul `alert`.
103103

104-
There can be more `else if` blocks. The final `else` is optional.
104+
Pot fi mai multe blocuri `else if`. Ultimul `else` este opțional.
105105

106-
## Conditional operator '?'
106+
## Operatorul condițional '?'
107107

108-
Sometimes, we need to assign a variable depending on a condition.
108+
Uneori, trebuie să atribuim o variabilă care depinde de o condiție.
109109

110-
For instance:
110+
De exemplu:
111111

112112
```js run no-beautify
113113
let accessAllowed;
114-
let age = prompt('How old are you?', '');
114+
let age = prompt('Câți ani ai?', '');
115115

116116
*!*
117117
if (age > 18) {
@@ -124,116 +124,116 @@ if (age > 18) {
124124
alert(accessAllowed);
125125
```
126126

127-
The so-called "conditional" or "question mark" operator lets us do that in a shorter and simpler way.
127+
Așa-numitul operator "condițional" sau "semn de întrebare" ne lasă să facem asta într-un mod mai scurt și simplu.
128128

129-
The operator is represented by a question mark `?`. Sometimes it's called "ternary", because the operator has three operands. It is actually the one and only operator in JavaScript which has that many.
129+
Operatorul este reprezentat de un semn de întrebare `?`. Uneori se numește "ternary", deoarece operatorul are trei operanzi. De fapt este singurul și unicul operator din JavaScript care are atât de mulți.
130130

131-
The syntax is:
131+
Sintaxa este:
132132
```js
133133
let result = condition ? value1 : value2;
134134
```
135135

136-
The `condition` is evaluated: if it's truthy then `value1` is returned, otherwise -- `value2`.
136+
Se evaluează `condition`: dacă este truthy atunci se returnează `value1`, altfel -- `value2`.
137137

138-
For example:
138+
De exemplu:
139139

140140
```js
141141
let accessAllowed = (age > 18) ? true : false;
142142
```
143143

144-
Technically, we can omit the parentheses around `age > 18`. The question mark operator has a low precedence, so it executes after the comparison `>`.
144+
Din punct de vedere tehnic, putem omite parantezele din jurul lui `age > 18`. Operatorul semn de întrebare are o precedență scăzută, deci se execută după comparația `>`.
145145

146-
This example will do the same thing as the previous one:
146+
Acest exemplu va face același lucru ca și cel precedent:
147147

148148
```js
149-
// the comparison operator "age > 18" executes first anyway
150-
// (no need to wrap it into parentheses)
149+
// operatorul de comparație "age > 18" se execută oricum primul
150+
// (nu este nevoie să îl punem între paranteze)
151151
let accessAllowed = age > 18 ? true : false;
152152
```
153153

154-
But parentheses make the code more readable, so we recommend using them.
154+
Dar parantezele fac codul mai ușor de citit, așa că vă recomandăm să le folosiți.
155155

156156
````smart
157-
In the example above, you can avoid using the question mark operator because the comparison itself returns `true/false`:
157+
În exemplul de mai sus, puteți evita utilizarea operatorului semn de întrebare deoarece comparația în sine returnează `true/false`:
158158
159159
```js
160-
// the same
160+
// același
161161
let accessAllowed = age > 18;
162162
```
163163
````
164164

165165
## Multiple '?'
166166

167-
A sequence of question mark operators `?` can return a value that depends on more than one condition.
167+
O secvență de operatori de semne de întrebare `?` poate returna o valoare care depinde de mai mult de o condiție.
168168

169-
For instance:
169+
De exemplu:
170170
```js run
171-
let age = prompt('age?', 18);
171+
let age = prompt('Vârsta?', 18);
172172

173-
let message = (age < 3) ? 'Hi, baby!' :
174-
(age < 18) ? 'Hello!' :
175-
(age < 100) ? 'Greetings!' :
176-
'What an unusual age!';
173+
let message = (age < 3) ? 'Bună, dragă!' :
174+
(age < 18) ? 'Bună!' :
175+
(age < 100) ? 'Salutări!' :
176+
'Ce vârstă neobișnuită!';
177177

178178
alert( message );
179179
```
180180

181-
It may be difficult at first to grasp what's going on. But after a closer look, we can see that it's just an ordinary sequence of tests:
181+
Ar putea fi dificil la început să înțelegem ce se întâmplă. Dar după o privire mai atentă, putem vedea că este doar o secvență obișnuită de teste:
182182

183-
1. The first question mark checks whether `age < 3`.
184-
2. If true -- it returns `'Hi, baby!'`. Otherwise, it continues to the expression after the colon '":"', checking `age < 18`.
185-
3. If that's true -- it returns `'Hello!'`. Otherwise, it continues to the expression after the next colon '":"', checking `age < 100`.
186-
4. If that's true -- it returns `'Greetings!'`. Otherwise, it continues to the expression after the last colon '":"', returning `'What an unusual age!'`.
183+
1. Primul semn de întrebare verifică dacă `age < 3`.
184+
2. Dacă este adevărat -- se returnează `'Bună, puiule!``. În caz contrar, continuă spre expresia de după două puncte '":"`, verificând `age < 18`.
185+
3. Dacă este adevărat -- returnează `'Bună!``. În caz contrar, continuă cu expresia de după următoarele două puncte '":"'', verificând `age < 100`.
186+
4. Dacă acest lucru este adevărat -- returnează `'Salutări!'`. În caz contrar, continuă cu expresia de după ultimele două puncte '":"'', returnând `'Ce vârstă neobișnuită!'`.
187187

188-
Here's how this looks using `if..else`:
188+
Iată cum arată acest lucru folosind `if..else`:
189189

190190
```js
191191
if (age < 3) {
192-
message = 'Hi, baby!';
192+
message = 'Bună, puiule!';
193193
} else if (age < 18) {
194-
message = 'Hello!';
194+
message = 'Bună!';
195195
} else if (age < 100) {
196-
message = 'Greetings!';
196+
message = "Salutări!";
197197
} else {
198-
message = 'What an unusual age!';
198+
message = "Ce vârstă neobișnuită!";
199199
}
200200
```
201201

202-
## Non-traditional use of '?'
202+
## Utilizarea netradițională a lui "?
203203

204-
Sometimes the question mark `?` is used as a replacement for `if`:
204+
Uneori semnul de întrebare `?` este folosit ca înlocuitor pentru `if`:
205205

206206
```js run no-beautify
207-
let company = prompt('Which company created JavaScript?', '');
207+
let company = prompt('Ce companie a creat JavaScript?', '');
208208

209209
*!*
210210
(company == 'Netscape') ?
211-
alert('Right!') : alert('Wrong.');
211+
alert('Corect!') : alert('Greșit.');
212212
*/!*
213213
```
214214

215-
Depending on the condition `company == 'Netscape'`, either the first or the second expression after the `?` gets executed and shows an alert.
215+
În funcție de condiția `company == 'Netscape'`, prima sau a doua expresie de după `?` se execută și afișează o alertă.
216216

217-
We don't assign a result to a variable here. Instead, we execute different code depending on the condition.
217+
Aici nu atribuim un rezultat unei variabile. În schimb, executăm un cod diferit în funcție de condiție.
218218

219-
**It's not recommended to use the question mark operator in this way.**
219+
**Nu este recomandat să folosim operatorul semn de întrebare în acest mod.**
220220

221-
The notation is shorter than the equivalent `if` statement, which appeals to some programmers. But it is less readable.
221+
Notația este mai scurtă decât instrucțiunea echivalentă `if`, ceea ce îi atrage pe unii programatori. Dar este mai puțin lizibilă.
222222

223-
Here is the same code using `if` for comparison:
223+
Iată același cod folosind `if` pentru comparație:
224224

225225
```js run no-beautify
226-
let company = prompt('Which company created JavaScript?', '');
226+
let company = prompt('Ce companie a creat JavaScript?', '');
227227

228228
*!*
229229
if (company == 'Netscape') {
230-
alert('Right!');
230+
alert('Corect!');
231231
} else {
232-
alert('Wrong.');
232+
alert('Greșit.');
233233
}
234234
*/!*
235235
```
236236

237-
Our eyes scan the code vertically. Code blocks which span several lines are easier to understand than a long, horizontal instruction set.
237+
Ochii noștri scanează codul pe verticală. Blocurile de cod care se întind pe câteva linii sunt mai ușor de înțeles decât un set de instrucțiuni lung și orizontal.
238238

239-
The purpose of the question mark operator `?` is to return one value or another depending on its condition. Please use it for exactly that. Use `if` when you need to execute different branches of code.
239+
Scopul operatorului cu semnul întrebării `?` este de a returna o valoare sau alta în funcție de condiția sa. Vă rugăm să îl folosiți exact pentru acest lucru. Folosiți `if` atunci când trebuie să executați diferite branșe de cod.

0 commit comments

Comments
 (0)