Skip to content

Commit 154d412

Browse files
authored
Merge pull request #82 from meilune/master
Translated Switch chapter
2 parents 566d151 + bd7652c commit 154d412

File tree

5 files changed

+73
-73
lines changed

5 files changed

+73
-73
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
To precisely match the functionality of `switch`, the `if` must use a strict comparison `'==='`.
1+
Tam, kad būtų palaikomas `switch` funkcionalumas `if` turi naudoti griežtą lygybę `'==='`.
22

3-
For given strings though, a simple `'=='` works too.
3+
Bet duotoms eilutėms, paprasta lygybė `'=='` taip pat veikia.
44

55
```js no-beautify
66
if(browser == 'Edge') {
7-
alert("You've got the Edge!");
7+
alert("Jūs turite Edge!");
88
} else if (browser == 'Chrome'
99
|| browser == 'Firefox'
1010
|| browser == 'Safari'
1111
|| browser == 'Opera') {
12-
alert( 'Okay we support these browsers too' );
12+
alert( 'Gerai, palaikome ir tokias naršykles' );
1313
} else {
14-
alert( 'We hope that this page looks ok!' );
14+
alert( 'Mes tikimės, kad šis puslapis veikia gerai!' );
1515
}
1616
```
1717

18-
Please note: the construct `browser == 'Chrome' || browser == 'Firefox' …` is split into multiple lines for better readability.
18+
Atkreipkite dėmesį: konstruktas `browser == 'Chrome' || browser == 'Firefox' …` yra atskirtas į naujas eiles dėl geresnio skaitomumo.
1919

20-
But the `switch` construct is still cleaner and more descriptive.
20+
Tačiau `switch` vis tiek yra švaresnis ir labiau apibūdinantis konstruktas.

Diff for: 1-js/02-first-steps/13-switch/1-rewrite-switch-if-else/task.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@ importance: 5
22

33
---
44

5-
# Rewrite the "switch" into an "if"
5+
# Perrašykite "switch" į "if"
66

7-
Write the code using `if..else` which would correspond to the following `switch`:
7+
Perrašykite kodą naudodami `if..else`, kuris atitiktų sekantį `switch`:
88

99
```js
1010
switch (browser) {
1111
case 'Edge':
12-
alert( "You've got the Edge!" );
12+
alert( "Jūs turite Edge!" );
1313
break;
1414

1515
case 'Chrome':
1616
case 'Firefox':
1717
case 'Safari':
1818
case 'Opera':
19-
alert( 'Okay we support these browsers too' );
19+
alert( 'Puiku, mes palaikome ir šias naršykles' );
2020
break;
2121

2222
default:
23-
alert( 'We hope that this page looks ok!' );
23+
alert( 'Mes tikimės, kad šis puslapis veikia gerai!' );
2424
}
2525
```
2626

Diff for: 1-js/02-first-steps/13-switch/2-rewrite-if-switch/solution.md

+3-3
Original file line numberDiff line numberDiff 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:
22

33
```js run
44
let a = +prompt('a?', '');
@@ -21,6 +21,6 @@ switch (a) {
2121
}
2222
```
2323

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.
2525

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.

Diff for: 1-js/02-first-steps/13-switch/2-rewrite-if-switch/task.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ importance: 4
22

33
---
44

5-
# Rewrite "if" into "switch"
5+
# Perrašykite "if" į "switch"
66

7-
Rewrite the code below using a single `switch` statement:
7+
Perrašykite kodą esantį žemiau naudodami vieną `switch` teiginį:
88

99
```js run
1010
let a = +prompt('a?', '');

Diff for: 1-js/02-first-steps/13-switch/article.md

+56-56
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
# The "switch" statement
1+
# Teiginys "switch"
22

3-
A `switch` statement can replace multiple `if` checks.
3+
Teiginys `switch` gali pakeisti daugybinius `if` patikrinimus.
44

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.
66

7-
## The syntax
7+
## Sintaksė
88

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ą.
1010

11-
It looks like this:
11+
Tai atrodo taip:
1212

1313
```js no-beautify
1414
switch(x) {
15-
case 'value1': // if (x === 'value1')
15+
case 'vertė1': // if (x === 'vertė1')
1616
...
1717
[break]
1818

19-
case 'value2': // if (x === 'value2')
19+
case 'vertė2': // if (x === 'vertė2')
2020
...
2121
[break]
2222

@@ -26,71 +26,71 @@ switch(x) {
2626
}
2727
```
2828

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).
3232

33-
## An example
33+
## Pavyzdys
3434

35-
An example of `switch` (the executed code is highlighted):
35+
Pavyzdys `switch` (įvykdytas kodas yra paryškintas):
3636

3737
```js run
3838
let a = 2 + 2;
3939

4040
switch (a) {
4141
case 3:
42-
alert( 'Too small' );
42+
alert( 'Per mažas' );
4343
break;
4444
*!*
4545
case 4:
46-
alert( 'Exactly!' );
46+
alert( 'Kaip tik!' );
4747
break;
4848
*/!*
4949
case 5:
50-
alert( 'Too large' );
50+
alert( 'Per didelis' );
5151
break;
5252
default:
53-
alert( "I don't know such values" );
53+
alert( "Tokios vertės nežinau" );
5454
}
5555
```
5656

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.
5858

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`.
6060

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ų.**
6262

63-
An example without `break`:
63+
Pavyzdys be `break`:
6464

6565
```js run
6666
let a = 2 + 2;
6767

6868
switch (a) {
6969
case 3:
70-
alert( 'Too small' );
70+
alert( 'Per mažas' );
7171
*!*
7272
case 4:
73-
alert( 'Exactly!' );
73+
alert( 'Kaip tik!' );
7474
case 5:
75-
alert( 'Too big' );
75+
alert( 'Per didelis' );
7676
default:
77-
alert( "I don't know such values" );
77+
alert( "Tokios vertės nežinau" );
7878
*/!*
7979
}
8080
```
8181

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`:
8383

8484
```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" );
8888
```
8989

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.
9292

93-
For example:
93+
Pavyzdžiui:
9494

9595
```js run
9696
let a = "1";
@@ -99,74 +99,74 @@ let b = 0;
9999
switch (+a) {
100100
*!*
101101
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");
103103
break;
104104
*/!*
105105

106106
default:
107-
alert("this doesn't run");
107+
alert("tai nepasileidžia");
108108
}
109109
```
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.
111111
````
112112
113-
## Grouping of "case"
113+
## Grupavimas su "case"
114114
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.
116116
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`:
118118
119119
```js run no-beautify
120120
let a = 2 + 2;
121121
122122
switch (a) {
123123
case 4:
124-
alert('Right!');
124+
alert('Teisingai!');
125125
break;
126126
127127
*!*
128-
case 3: // (*) grouped two cases
128+
case 3: // (*) dvi bylos sugrupuotos
129129
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.');
132132
break;
133133
*/!*
134134
135135
default:
136-
alert('The result is strange. Really.');
136+
alert('Rezultatas yra iš tikrųjų keistas.');
137137
}
138138
```
139139
140-
Now both `3` and `5` show the same message.
140+
Dabar abu `3` ir `5` parodo tą pačią žinutę.
141141
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`.
143143
144-
## Type matters
144+
## Tipas yra svarbu
145145
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ą.
147147
148-
For example, let's consider the code:
148+
Pavyzdžiui, apsvarstykime tokį kodą:
149149
150150
```js run
151-
let arg = prompt("Enter a value?");
151+
let arg = prompt("Įveskite vertę?");
152152
switch (arg) {
153153
case '0':
154154
case '1':
155-
alert( 'One or zero' );
155+
alert( 'Vienas arba nulis' );
156156
break;
157157
158158
case '2':
159-
alert( 'Two' );
159+
alert( 'Du' );
160160
break;
161161
162162
case 3:
163-
alert( 'Never executes!' );
163+
alert( 'Niekada nėra įvykdomas!' );
164164
break;
165165
default:
166-
alert( 'An unknown value' );
166+
alert( 'Nežinoma vertė' );
167167
}
168168
```
169169
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

Comments
 (0)