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/02-first-steps/13-switch/2-rewrite-if-switch/solution.md
+3-3
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
The first two checks turn into two `case`. The third check is split into two cases:
1
+
Pirmi du patikrinimai pavirsta į dvi atskiras bylas `case`. Trečias patikrinimas išskiriamas į dvi bylas:
2
2
3
3
```js run
4
4
let a =+prompt('a?', '');
@@ -21,6 +21,6 @@ switch (a) {
21
21
}
22
22
```
23
23
24
-
Please note: the `break`at the bottom is not required. But we put it to make the code future-proof.
24
+
Atkreipkite dėmesį: pabaigoje esantis `break`nėra reikalingas. Bet mes jį pridedame tam, kad kodas būtų paruoštas ateities pakeitimams.
25
25
26
-
In the future, there is a chance that we'd want to add one more `case`, for example `case 4`. And if we forget to add a break before it, at the end of `case 3`, there will be an error. So that's a kind of self-insurance.
26
+
Yra galimybė, kad ateityje norėsime pridėti dar vieną bylą `case`, pavyzdžiui `case 4`. Jeigu prieš tai būsime pamiršę pridėti `break`, tokiu atveju `case 3` pabaigoje bus klaida. Tad tai yra mūsų pačių apsidraudimas.
A`switch`statement can replace multiple `if`checks.
3
+
Teiginys`switch`gali pakeisti daugybinius `if`patikrinimus.
4
4
5
-
It gives a more descriptive way to compare a value with multiple variants.
5
+
Jis suteikia lengviau apibūdinamą kelią palyginti vertes su įvairiais variantais.
6
6
7
-
## The syntax
7
+
## Sintaksė
8
8
9
-
The`switch`has one or more`case`blocks and an optional default.
9
+
Teiginys`switch`turi vieną ir daugiau`case`(bylos) blokų ir numatytąjį pasirinkimą.
10
10
11
-
It looks like this:
11
+
Tai atrodo taip:
12
12
13
13
```js no-beautify
14
14
switch(x) {
15
-
case'value1': // if (x === 'value1')
15
+
case'vertė1': // if (x === 'vertė1')
16
16
...
17
17
[break]
18
18
19
-
case'value2': // if (x === 'value2')
19
+
case'vertė2': // if (x === 'vertė2')
20
20
...
21
21
[break]
22
22
@@ -26,71 +26,71 @@ switch(x) {
26
26
}
27
27
```
28
28
29
-
-The value of `x`is checked for a strict equality to the value from the first `case` (that is, `value1`) then to the second (`value2`) and so on.
30
-
-If the equality is found, `switch`starts to execute the code starting from the corresponding `case`, until the nearest `break` (or until the end of`switch`).
31
-
-If no case is matched then the `default`code is executed (if it exists).
29
+
-Vertė `x`yra patikrinama griežta lygybe su verte iš pirmos bylos `case` (tai yra `vertė1`) tada su antra (`vertė2`) ir taip toliau.
30
+
-Jeigu lygybė randama, `switch`pradeda vykdyti kodą nuo atitinkančios bylos `case`, iki artimiausio `break` (arba kol pasibaigs`switch`).
31
+
-Jeigu nei viena byla nesurado atitikimo, tokiu atveju yra įvykdomas `default`kodas (jeigu toks egzistuoja).
32
32
33
-
## An example
33
+
## Pavyzdys
34
34
35
-
An example of `switch` (the executed code is highlighted):
35
+
Pavyzdys `switch` (įvykdytas kodas yra paryškintas):
36
36
37
37
```js run
38
38
let a =2+2;
39
39
40
40
switch (a) {
41
41
case3:
42
-
alert( 'Too small' );
42
+
alert( 'Per mažas' );
43
43
break;
44
44
*!*
45
45
case4:
46
-
alert( 'Exactly!' );
46
+
alert( 'Kaip tik!' );
47
47
break;
48
48
*/!*
49
49
case5:
50
-
alert( 'Too large' );
50
+
alert( 'Per didelis' );
51
51
break;
52
52
default:
53
-
alert( "I don't know such values" );
53
+
alert( "Tokios vertės nežinau" );
54
54
}
55
55
```
56
56
57
-
Here the`switch`starts to compare `a`from the first`case`variant that is`3`. The match fails.
57
+
Šiuo atveju`switch`pradeda lyginti `a`iš pirmos bylos`case`variantą, kuris yra`3`. Atitikmuo nėra randamas.
58
58
59
-
Then`4`. That's a match, so the execution starts from `case 4`until the nearest`break`.
59
+
Tada`4`. Atitikmuo randamas, tad pradedamas vykdymas nuo `case 4`iki artimiausio`break`.
60
60
61
-
**If there is no `break`then the execution continues with the next `case`without any checks.**
61
+
**Jeigu nėra `break`tokiu atveju vykdymas tęsiasi su sekančia `case`be jokių patikrinimų.**
62
62
63
-
An example without`break`:
63
+
Pavyzdys be`break`:
64
64
65
65
```js run
66
66
let a =2+2;
67
67
68
68
switch (a) {
69
69
case3:
70
-
alert( 'Too small' );
70
+
alert( 'Per mažas' );
71
71
*!*
72
72
case4:
73
-
alert( 'Exactly!' );
73
+
alert( 'Kaip tik!' );
74
74
case5:
75
-
alert( 'Too big' );
75
+
alert( 'Per didelis' );
76
76
default:
77
-
alert( "I don't know such values" );
77
+
alert( "Tokios vertės nežinau" );
78
78
*/!*
79
79
}
80
80
```
81
81
82
-
In the example above we'll see sequential execution of three `alert`s:
82
+
Pavyzdyje aukščiau iš eilės matome kaip įvykdomi trys `alert`:
83
83
84
84
```js
85
-
alert( 'Exactly!' );
86
-
alert( 'Too big' );
87
-
alert( "I don't know such values" );
85
+
alert( 'Kaip tik!' );
86
+
alert( 'Per didelis' );
87
+
alert( "Tokios vertės nežinau" );
88
88
```
89
89
90
-
````smart header="Any expression can be a`switch/case`argument"
91
-
Both`switch`and`case`allow arbitrary expressions.
90
+
````smart header="Bet kokia išraiška gali būti`switch/case`argumentu"
91
+
Abu`switch`ir`case`leidžia sutartines išraiškas.
92
92
93
-
For example:
93
+
Pavyzdžiui:
94
94
95
95
```js run
96
96
let a ="1";
@@ -99,74 +99,74 @@ let b = 0;
99
99
switch (+a) {
100
100
*!*
101
101
case b +1:
102
-
alert("this runs, because +a is 1, exactly equals b+1");
102
+
alert("šitas preina, nes +a yra 1, tiksliai lygu b+1");
103
103
break;
104
104
*/!*
105
105
106
106
default:
107
-
alert("this doesn't run");
107
+
alert("tai nepasileidžia");
108
108
}
109
109
```
110
-
Here`+a`gives`1`, that's compared with `b + 1`in`case`, and the corresponding code is executed.
110
+
Čia`+a`atiduoda`1`, tai yra palyginama su `b + 1`byloje`case`, įvykdomas atitinkamas kodas.
111
111
````
112
112
113
-
## Grouping of "case"
113
+
## Grupavimas su "case"
114
114
115
-
Several variants of `case` which share the same code can be grouped.
115
+
Keli `case` variantai, kurie dalinasi tuo pačiu kodu gali būti sugrupuoti.
116
116
117
-
For example, if we want the same code to run for `case 3` and `case 5`:
117
+
Pavyzdžiui, jeigu mes norime, kad tas pats kodas pasileistų byloms `case 3` ir `case 5`:
118
118
119
119
```js run no-beautify
120
120
let a = 2 + 2;
121
121
122
122
switch (a) {
123
123
case 4:
124
-
alert('Right!');
124
+
alert('Teisingai!');
125
125
break;
126
126
127
127
*!*
128
-
case 3: // (*) grouped two cases
128
+
case 3: // (*) dvi bylos sugrupuotos
129
129
case 5:
130
-
alert('Wrong!');
131
-
alert("Why don't you take a math class?");
130
+
alert('Neteisingai!');
131
+
alert('Būtų neblogai apsilankyti matematikos pamokoje.');
132
132
break;
133
133
*/!*
134
134
135
135
default:
136
-
alert('The result is strange. Really.');
136
+
alert('Rezultatas yra iš tikrųjų keistas.');
137
137
}
138
138
```
139
139
140
-
Now both `3` and `5` show the same message.
140
+
Dabar abu `3` ir `5` parodo tą pačią žinutę.
141
141
142
-
The ability to "group" cases is a side-effect of how `switch/case` works without `break`. Here the execution of `case 3` starts from the line `(*)` and goes through `case 5`, because there's no `break`.
142
+
Gebėjimas "sugrupuoti" bylas yra šalutinis efektas to kaip `switch/case` veikia be `break`. Čia `case 3` vykdymas prasideda nuo eilės su `(*)` ir eina per bylą `case 5`, nes nėra `break`.
143
143
144
-
## Type matters
144
+
## Tipas yra svarbu
145
145
146
-
Let's emphasize that the equality check is always strict. The values must be of the same type to match.
146
+
Pabrėžkime tai, kad lygybės patikrinimas yra visada griežtas. Vertės turi būti vienodo tipo, kad atitiktų viena kitą.
147
147
148
-
For example, let's consider the code:
148
+
Pavyzdžiui, apsvarstykime tokį kodą:
149
149
150
150
```js run
151
-
let arg = prompt("Enter a value?");
151
+
let arg = prompt("Įveskite vertę?");
152
152
switch (arg) {
153
153
case '0':
154
154
case '1':
155
-
alert( 'One or zero' );
155
+
alert( 'Vienas arba nulis' );
156
156
break;
157
157
158
158
case '2':
159
-
alert( 'Two' );
159
+
alert( 'Du' );
160
160
break;
161
161
162
162
case 3:
163
-
alert( 'Never executes!' );
163
+
alert( 'Niekada nėra įvykdomas!' );
164
164
break;
165
165
default:
166
-
alert( 'An unknown value' );
166
+
alert( 'Nežinoma vertė' );
167
167
}
168
168
```
169
169
170
-
1. For `0`, `1`, the first `alert` runs.
171
-
2. For `2` the second `alert` runs.
172
-
3. But for `3`, the result of the `prompt` is a string `"3"`, which is not strictly equal `===` to the number `3`. So we've got a dead code in `case 3`! The `default` variant will execute.
170
+
1. Kai įvedamas `0`, `1`, paleidžiamas pirmas `alert`.
171
+
2. Kai įvedamas `2` paleidžiamas antras `alert`.
172
+
3. Bet `3` neįvykdomas, kadangi `prompt` rezultatas yra eilutė `"3"`, o tai nėra griežtai lygu `===` skaičiui `3`. Tad mes turime neveikiantį kodą byloje `case 3`! Įvykdomas `default` variantas.
0 commit comments