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
Sometimes, we need to perform different actions based on different conditions.
3
+
Kartais mums tenka atlikti skirtingus veiksmus atitinkančius skirtingas sąlygas.
4
4
5
-
To do that, we can use the `if`statement and the conditional operator `?`, that's also called a "question mark" operator.
5
+
Kad tai padarytume mes naudojame teiginį `if`ir sąlyginį operatorių `?`, arba dar kitaip vadinamą "klaustuko" operatorių.
6
6
7
-
## The "if" statement
7
+
## Teiginys "if"
8
8
9
-
The`if(...)`statement evaluates a condition in parentheses and, if the result is `true`, executes a block of code.
9
+
Teiginys`if(...)`įvertina sąlygas esančias tarp skliaustelių ir jeigu rezultatas yra `true` tada įvykdo kodų rinkinį.
10
10
11
-
For example:
11
+
Pavyzdžiui:
12
12
13
13
```js run
14
-
let year =prompt('In which year was ECMAScript-2015 specification published?', '');
14
+
let year =prompt('Kuriais metais buvo išleista ECMAScript-2015 specifikacija?', '');
15
15
16
16
*!*
17
-
if (year ==2015) alert( 'You are right!' );
17
+
if (year ==2015) alert( 'Jūs teisi(-us)!' );
18
18
*/!*
19
19
```
20
20
21
-
In the example above, the condition is a simple equality check (`year == 2015`), but it can be much more complex.
21
+
Pavyzdyje viršuje, sąlyga yra paprastas lygybės patikrinimas (`year == 2015`), bet jis gali būti daug sudėtingesnis.
22
22
23
-
If we want to execute more than one statement, we have to wrap our code block inside curly braces:
23
+
Jeigu norime įvykdyti daugiau nei vieną teiginį, turime kodų rinkinį apskliausti riestiniais skliaustais:
24
24
25
25
```js
26
26
if (year ==2015) {
27
-
alert( "That's correct!" );
28
-
alert( "You're so smart!" );
27
+
alert( "Jūs teisi(-us)!" );
28
+
alert( "Jūs protinga(-as)!" );
29
29
}
30
30
```
31
31
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
+
Rekomenduojame apskliausti kodų rinkinį su riestiniais skliaustais `{}`kiekvieną kartą kai pamatote `if`, net kai tereikia įvykdyti tik vieną teiginį. Taip darydami palengvinsite skaitomumą.
33
33
34
-
## Boolean conversion
34
+
## Loginės konversijos
35
35
36
-
The`if (…)`statement evaluates the expression in its parentheses and converts the result to a boolean.
36
+
Teiginys`if (…)`įvertina išraišką esančią skliaustuose ir paverčia rezultatą į loginę vertę.
37
37
38
-
Let's recall the conversion rules from the chapter<info:type-conversions>:
38
+
Prisiminkime konversijos taisykles iš skyriaus<info:type-conversions>:
39
39
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
+
-Skaičius `0`, tuščia eilutė `""`, `null`, `undefined` ir`NaN`visi tampa`false`. Nes jos yra vadinamosios "falsy" (neteisingosio) vertės.
41
+
-Kitos vertės tampa`true`, tad jos vadinamos "truthy" (teisingosios).
42
42
43
-
So, the code under this condition would never execute:
43
+
Taigi kodas tokiu atveju niekada nebūtų įvykdytas:
44
44
45
45
```js
46
-
if (0) { // 0 is falsy
46
+
if (0) { // 0 yra falsy
47
47
...
48
48
}
49
49
```
50
50
51
-
...and inside this condition -- it always will:
51
+
...o šios sąlygos viduje -- visada įvykdomas:
52
52
53
53
```js
54
-
if (1) { // 1 is truthy
54
+
if (1) { // 1 yra truthy
55
55
...
56
56
}
57
57
```
58
58
59
-
We can also pass a pre-evaluated boolean value to `if`, like this:
59
+
Mes taip pat galime praleisti iš anksto įvertintą loginę vertę per `if`, štai taip:
60
60
61
61
```js
62
-
let cond = (year ==2015); //equality evaluates to true or false
62
+
let cond = (year ==2015); //lygybė įvertinama į true arba false
63
63
64
64
if (cond) {
65
65
...
66
66
}
67
67
```
68
68
69
-
## The "else" clause
69
+
## Išlyga "else"
70
70
71
-
The`if`statement may contain an optional "else" block. It executes when the condition is false.
71
+
Teiginys`if`gali turėti išlygos "else" rinkinį. Jis įvykdomas kai sąlyga yra false.
72
72
73
-
For example:
73
+
Pavyzdžiui:
74
74
```js run
75
-
let year =prompt('In which year was the ECMAScript-2015 specification published?', '');
75
+
let year =prompt('Kuriais metais buvo išleista ECMAScript-2015 specifikacija?', '');
76
76
77
77
if (year ==2015) {
78
-
alert( 'You guessed it right!' );
78
+
alert( 'Jūs atspėjote!' );
79
79
} else {
80
-
alert( 'How can you be so wrong?' ); //any value except 2015
80
+
alert( 'Kaip galėjote taip suklysti?' ); //bet kuriai kitai vertei išskyrus 2015
81
81
}
82
82
```
83
83
84
-
## Several conditions: "else if"
84
+
## Kelios sąlygos: "else if"
85
85
86
-
Sometimes, we'd like to test several variants of a condition. The`else if`clause lets us do that.
86
+
Kartais mums gali tekti testuoti kelis sąlygos variantus. Išlyga`else if`leidžia tai padaryti.
87
87
88
-
For example:
88
+
Pavyzdžiui:
89
89
90
90
```js run
91
-
let year =prompt('In which year was the ECMAScript-2015 specification published?', '');
91
+
let year =prompt('Kuriais metais buvo išleista ECMAScript-2015 specifikacija?', '');
92
92
93
93
if (year <2015) {
94
-
alert( 'Too early...' );
94
+
alert( 'Per anksti...' );
95
95
} elseif (year >2015) {
96
-
alert( 'Too late' );
96
+
alert( 'Per vėlai' );
97
97
} else {
98
-
alert( 'Exactly!' );
98
+
alert( 'Būtent!' );
99
99
}
100
100
```
101
101
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
+
JavaScript kode aukščiau visų pirma patikrina `year < 2015`. Jeigu jis yra falsy, ji pereina prie kitos sąlygos `year > 2015`. Jeigu ji yra falsy, parodo paskutinį`alert`.
103
103
104
-
There can be more `else if`blocks. The final `else`is optional.
104
+
Gali būti daug `else if`rinkinių. Paskutinis `else`nėra privalomas.
105
105
106
-
## Conditional operator '?'
106
+
## Sąlyginis operatorius '?'
107
107
108
-
Sometimes, we need to assign a variable depending on a condition.
108
+
Kartais mes turime priskirti kintamąjį priklausomai nuo sąlygos.
109
109
110
-
For instance:
110
+
Pavyzdžiui:
111
111
112
112
```js run no-beautify
113
113
let accessAllowed;
114
-
let age =prompt('How old are you?', '');
114
+
let age =prompt('Kiek jums metų?', '');
115
115
116
116
*!*
117
117
if (age >18) {
@@ -124,116 +124,116 @@ if (age > 18) {
124
124
alert(accessAllowed);
125
125
```
126
126
127
-
The so-called "conditional" or "question mark" operator lets us do that in a shorter and simpler way.
127
+
Taip vadinamas "sąlyginis" arba "klaustuko" operatorius mums leidžia tai padaryti daug trumpesniu ir paprastesniu būdu.
128
128
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
+
Operatorių atstovauja klaustukas `?`. Kartais jis vadinamas "ternariniu", nes operatorius turi tris operandus. Iš tikrųjų jis vienas ir vienintelis, kuris JavaScript turi tiek operandų.
130
130
131
-
The syntax is:
131
+
Sintaksė yra tokia:
132
132
```js
133
133
let result = condition ? value1 : value2;
134
134
```
135
135
136
-
The`condition`is evaluated: if it's truthy then `value1` is returned, otherwise -- `value2`.
136
+
Sąlyga`condition`yra įvertinama: jeigu ji yra truthy tada grąžinama vertė `value1`, kitu atveju -- `value2`.
137
137
138
-
For example:
138
+
Pavyzdžiui:
139
139
140
140
```js
141
141
let accessAllowed = (age >18) ?true:false;
142
142
```
143
143
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
+
Techniškai galime nedėti skliaustelių aplink `age > 18`. Klaustukas turi žemą prioritetą, tad jis įvykdomas po palyginimo`>`.
145
145
146
-
This example will do the same thing as the previous one:
146
+
Šis palyginimas darys tą patį kaip aukščiau esantis:
147
147
148
148
```js
149
-
//the comparison operator "age > 18" executes first anyway
150
-
// (no need to wrap it into parentheses)
149
+
//palyginimo operatorius "age > 18" įvykdomas pirmas
150
+
// (nebūtina skliausti lenktiniais skliaustais)
151
151
let accessAllowed = age >18?true:false;
152
152
```
153
153
154
-
But parentheses make the code more readable, so we recommend using them.
154
+
Tačiau skliaustai daro kodą labiau įskaitomu, tad mes rekomenduojame juos naudoti.
155
155
156
156
````smart
157
-
In the example above, you can avoid using the question mark operator because the comparison itself returns `true/false`:
157
+
Pavyzdyje viršuje galite išvengti klaustuko, nes palyginimas bet kokiu atveju grąžina `true/false`:
158
158
159
159
```js
160
-
// the same
160
+
// tas pats
161
161
let accessAllowed = age > 18;
162
162
```
163
163
````
164
164
165
-
## Multiple '?'
165
+
## Daugkartiniai '?'
166
166
167
-
A sequence of question mark operators `?`can return a value that depends on more than one condition.
167
+
Seka klaustukų operatorių `?`gali grąžinti vertę, kuri priklauso nuo daugiau nei vienos sąlygos.
168
168
169
-
For instance:
169
+
Pavyzdžiui:
170
170
```js run
171
-
let age =prompt('age?', 18);
171
+
let age =prompt('amžius?', 18);
172
172
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) ?'Labas, kūdikėli!':
174
+
(age <18) ?'Labas!':
175
+
(age <100) ?'Sveiki!':
176
+
'Koks neįprastas amžius!';
177
177
178
178
alert( message );
179
179
```
180
180
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
+
Iš praždių gali būti sunku suprasti kas vyksta. Bet geriau įsižiūrėjus matome, kad tai tik įprastinė testų seka:
182
182
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.Pirmas klaustukas patikrina ar`age < 3`.
184
+
2.Jeigu true -- grąžina `'Labas, kūdikėli!'`. Kitu atveju, pereina prie išraiškos po dvitaškio '":"' ir tikrina`age < 18`.
185
+
3.Jeigu šitas yra true -- grąžina `'Labas!'`. Kitu atveju, pereina prie išraiškos po dvitaškio '":"' ir tikrina`age < 100`.
186
+
4.Jeigu tai true -- grąžina `'Sveiki!'`. Kitu atveju, pereina prie išraiškos po paskutinio dvitaškio '":"', grąžina`'Koks neįprastas amžius!'`.
187
187
188
-
Here's how this looks using`if..else`:
188
+
Štai kaip tai atrodytų, jeigu naudotume`if..else`:
189
189
190
190
```js
191
191
if (age <3) {
192
-
message ='Hi, baby!';
192
+
message ='Labas, kūdikėli!';
193
193
} elseif (age <18) {
194
-
message ='Hello!';
194
+
message ='Labas!';
195
195
} elseif (age <100) {
196
-
message ='Greetings!';
196
+
message ='Sveiki!';
197
197
} else {
198
-
message ='What an unusual age!';
198
+
message ='Koks neįprastas amžius!';
199
199
}
200
200
```
201
201
202
-
## Non-traditional use of '?'
202
+
## Netradicinis '?' naudojimas
203
203
204
-
Sometimes the question mark `?`is used as a replacement for`if`:
204
+
Kartais klaustukas `?`naudojamas kaip pakaitalas`if`:
205
205
206
206
```js run no-beautify
207
-
let company =prompt('Which company created JavaScript?', '');
207
+
let company =prompt('Kuri kompanija sukūrė JavaScript?', '');
208
208
209
209
*!*
210
210
(company =='Netscape') ?
211
-
alert('Right!') :alert('Wrong.');
211
+
alert('Teisingai!') :alert(Neteisingai.');
212
212
*/!*
213
213
```
214
214
215
-
Depending on the condition `company == 'Netscape'`, either the first or the second expression after the `?`gets executed and shows an alert.
215
+
Priklausomai nuo sąlygos `company == 'Netscape'`, arba pirma, arba antra išraiška po `?` bus įvykdyta ir parodys alert.
216
216
217
-
We don't assign a result to a variable here. Instead, we execute different code depending on the condition.
217
+
Šiuo atveju rezultatas nėra priskiriamas kintamajam. Vietoje to mes įvykdome atitinkamą kodą priklausomai nuo sąlygos.
218
218
219
-
**It's not recommended to use the question mark operator in this way.**
219
+
**Nerekomenduojama naudoti klaustuko tokiu būdu.**
220
220
221
-
The notation is shorter than the equivalent `if`statement, which appeals to some programmers. But it is less readable.
221
+
Toks žymėjimas yra trumpesnė teiginio`if` versija, o tai yra patrauklu kai kuriems programuotojams. Bet ji yra sunkiau perskaitoma.
222
222
223
-
Here is the same code using `if` for comparison:
223
+
Štai palyginimui toks pats kodas, bet naudojant `if`:
224
224
225
225
```js run no-beautify
226
-
let company =prompt('Which company created JavaScript?', '');
226
+
let company = prompt('Kuri kompanija sukūrė JavaScript?', '');
227
227
228
228
*!*
229
229
if (company == 'Netscape') {
230
-
alert('Right!');
230
+
alert('Teisingai!');
231
231
} else {
232
-
alert('Wrong.');
232
+
alert('Neteisingai.');
233
233
}
234
234
*/!*
235
235
```
236
236
237
-
Our eyes scan the code vertically. Code blocks which span several lines are easier to understand than a long, horizontal instruction set.
237
+
Mūsų akys skanuoja kodą vertikaliai. Kodų rinkinius, kurie išsiplečia per kelias eiles, daug lengviau perskaityti negu ilgus horizontalius instrukcijų rinkinius.
238
238
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
+
Klaustuko operatoriaus `?` tikslas yra grąžinti vieną arba kitą vertę priklausomai nuo sąlygos. Prašau naudokite jį būtent tam. Naudokite `if` kai jums reikia įvykdyti kodą su skirtingai išsišakojimais.
0 commit comments