Skip to content

Commit a9fb4c7

Browse files
authored
Merge pull request #70 from meilune/master
Translated Type Conversion Article
2 parents bef78ac + 78bacd5 commit a9fb4c7

File tree

3 files changed

+71
-71
lines changed

3 files changed

+71
-71
lines changed

1-js/02-first-steps/06-type-conversions/1-primitive-conversions-questions/solution.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ undefined + 1 = NaN // (6)
1717
" \t \n" - 2 = -2 // (7)
1818
```
1919

20-
1. The addition with a string `"" + 1` converts `1` to a string: `"" + 1 = "1"`, and then we have `"1" + 0`, the same rule is applied.
21-
2. The subtraction `-` (like most math operations) only works with numbers, it converts an empty string `""` to `0`.
22-
3. The addition with a string appends the number `5` to the string.
23-
4. The subtraction always converts to numbers, so it makes `" -9 "` a number `-9` (ignoring spaces around it).
24-
5. `null` becomes `0` after the numeric conversion.
25-
6. `undefined` becomes `NaN` after the numeric conversion.
26-
7. Space characters, are trimmed off string start and end when a string is converted to a number. Here the whole string consists of space characters, such as `\t`, `\n` and a "regular" space between them. So, similarly to an empty string, it becomes `0`.
20+
1. Sudėtis su eilute `"" + 1` paverčia `1` į eilutę: `"" + 1 = "1"`, o tada mes turime `"1" + 0`, taikoma ta pati taisyklė.
21+
2. Atimtis `-` (kaip ir didžioji dalis matematinių operacijų) veikia tik su skaičiais, tad tuščią eilutę `""` paverčia į `0`.
22+
3. Sudėtis su eilute prijungia skaičių `5` prie eilutės.
23+
4. Atimtis visada paverčia į numerius, tad `" -9 "` tampa numeriu `-9` (ignoruoja tarpus aplink).
24+
5. `null` tampa `0` po skaičių konversijos.
25+
6. `undefined` tampa `NaN` po skaičių konversijos.
26+
7. Tarpų ženklai yra nukerpami nuo eilutės pradžios ir pabaigos kai eilutė paverčiama į skaičių. Čia visa eilutė susideda iš tarpo ženklų kaip `\t`, `\n` ir "įprastinių" tarpų esančių tarp jų. Tad panašiai kaip ir tuščia eilutė, ji tampa `0`.

1-js/02-first-steps/06-type-conversions/1-primitive-conversions-questions/task.md

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

33
---
44

5-
# Type conversions
5+
# Tipų konversijos
66

7-
What are results of these expressions?
7+
Kokie yra šių išraiškų rezultatai?
88

99
```js no-beautify
1010
"" + 1 + 0
@@ -24,4 +24,4 @@ undefined + 1
2424
" \t \n" - 2
2525
```
2626

27-
Think well, write down and then compare with the answer.
27+
Gerai pagalvokite, užsirašykite ir palyginkite su atsakymais.
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,111 @@
1-
# Type Conversions
1+
# Tipų konversijos
22

3-
Most of the time, operators and functions automatically convert the values given to them to the right type.
3+
Dažniausiai operatoriai ir funkcijos automatiškai pakeičia jiems duotas vertes į teisingą tipą.
44

5-
For example, `alert` automatically converts any value to a string to show it. Mathematical operations convert values to numbers.
5+
Pavyzdžiui `alert` automatiškai paverčia bet kokią jiems duotą vertę į eilutės tipą, kad galėtų jį parodytų. Matematinės operacijos pakeičia vertes į skaičius.
66

7-
There are also cases when we need to explicitly convert a value to the expected type.
7+
Yra tokių konkrečių atvejų kai mums reikia vertę pakeisti į atitinkamą tipą.
88

9-
```smart header="Not talking about objects yet"
10-
In this chapter, we won't cover objects. Instead, we'll study primitives first. Later, after we learn about objects, we'll see how object conversion works in the chapter <info:object-toprimitive>.
9+
```smart header="Dar nekalbame apie objektus"
10+
Šiame skyriuje kol kas dar nekalbėsime apie objektus. Vietoje to studijuosime primityvius tipus. Vėliau kai sužinosime daugiau apie objektus, pamatysime kaip objektų keitimai veikia skyriuje <info:object-toprimitive>.
1111
```
1212

13-
## String Conversion
13+
## Eilutės konversijos
1414

15-
String conversion happens when we need the string form of a value.
15+
Eilutės keitimas įvyksta tada kai mums reikia eilutės formos vertės.
1616

17-
For example, `alert(value)` does it to show the value.
17+
Pavyzdžiui, `alert(value)` tai padaro, kad parodytų vertę.
1818

19-
We can also call the `String(value)` function to convert a value to a string:
19+
Mes taip pat galime iššaukti `String(value)` funkciją, kad konvertuotume vertę į eilutę:
2020

2121
```js run
2222
let value = true;
23-
alert(typeof value); // boolean
23+
alert(typeof value); // boolean (loginis)
2424

2525
*!*
26-
value = String(value); // now value is a string "true"
26+
value = String(value); // dabar vertė yra eilutė "true"
2727
alert(typeof value); // string
2828
*/!*
2929
```
3030

31-
String conversion is mostly obvious. A `false` becomes `"false"`, `null` becomes `"null"`, etc.
31+
Eilutės konversijos dažniausiai yra labai akivaizdžios. `false` tampa `"false"`, `null` tampa `"null"` ir t.t.
3232

33-
## Numeric Conversion
33+
## Skaičių konversijos
3434

35-
Numeric conversion happens in mathematical functions and expressions automatically.
35+
Skaičių konversijos įvyksta automatiškai matematinėse funkcijose ir formulėse.
3636

37-
For example, when division `/` is applied to non-numbers:
37+
Pavyzdžiui, kai dalyba `/` taikoma ne skaičiams:
3838

3939
```js run
40-
alert( "6" / "2" ); // 3, strings are converted to numbers
40+
alert( "6" / "2" ); // 3, eilutės paverčiamos skaičiais
4141
```
4242

43-
We can use the `Number(value)` function to explicitly convert a `value` to a number:
43+
Mes taip pat galime naudoti `Number(value)` funkciją konkrečiai tam, kad paverstume `value` į skaičių:
4444

4545
```js run
4646
let str = "123";
4747
alert(typeof str); // string
4848

49-
let num = Number(str); // becomes a number 123
49+
let num = Number(str); // tampa numeriu 123
5050

5151
alert(typeof num); // number
5252
```
5353

54-
Explicit conversion is usually required when we read a value from a string-based source like a text form but expect a number to be entered.
54+
Akivaizdžios konversijos dažniausiai reikalingos kai gauname vertę eilutės tipo formatu iš tekstinių šaltinių, bet mums iš tikrųjų reikalingas skaičius.
5555

56-
If the string is not a valid number, the result of such a conversion is `NaN`. For instance:
56+
Jeigu eilutė nėra tinkamas skaičius, tada tokios konversijos rezultatas bus is `NaN`. Pavyzdžiui:
5757

5858
```js run
59-
let age = Number("an arbitrary string instead of a number");
59+
let age = Number("arbitriška eilutė vietoje skaičiaus");
6060

61-
alert(age); // NaN, conversion failed
61+
alert(age); // NaN, konversija nepavyko
6262
```
6363

64-
Numeric conversion rules:
64+
Skaičių konversijos taisyklės:
6565

66-
| Value | Becomes... |
66+
| Vertė | Tampa... |
6767
|-------|-------------|
6868
|`undefined`|`NaN`|
6969
|`null`|`0`|
70-
|<code>true&nbsp;and&nbsp;false</code> | `1` and `0` |
71-
| `string` | Whitespaces from the start and end are removed. If the remaining string is empty, the result is `0`. Otherwise, the number is "read" from the string. An error gives `NaN`. |
70+
|<code>true&nbsp;ir&nbsp;false</code> | `1` ir `0` |
71+
| `string` | Tarpai pradžioje ir pabaigoje panaikinami. Jeigu likusi eilutė yra tuščia, rezultatas yra `0`. Kitu atveju, skaičius "perskaitomas" iš eilutės. Klaida grąžina `NaN`. |
7272

73-
Examples:
73+
Pavyzdžiai:
7474

7575
```js run
7676
alert( Number(" 123 ") ); // 123
77-
alert( Number("123z") ); // NaN (error reading a number at "z")
77+
alert( Number("123z") ); // NaN (klaida perskaitė skaičiuje "z")
7878
alert( Number(true) ); // 1
7979
alert( Number(false) ); // 0
8080
```
8181

82-
Please note that `null` and `undefined` behave differently here: `null` becomes zero while `undefined` becomes `NaN`.
82+
Atkreipkite dėmesį, kad `null` ir `undefined` elgiasi kitaip šiuo atveju: `null` tampa nuliu kai `undefined` tampa `NaN`.
8383

84-
````smart header="Addition '+' concatenates strings"
85-
Almost all mathematical operations convert values to numbers. A notable exception is addition `+`. If one of the added values is a string, the other one is also converted to a string.
84+
````smart header="Sudėtis '+' sujungia eilutes"
85+
Beveik visos matematinės operacijos paverčia vertes numeriais. Svarbi išimtis yra sudėtis `+`. Jeigu viena iš verčių yra eilutės, kita taip pat paverčiama eilute.
8686
87-
Then, it concatenates (joins) them:
87+
Tada ji sujungia (ang. concatenates) vertes:
8888
8989
```js run
90-
alert( 1 + '2' ); // '12' (string to the right)
91-
alert( '1' + 2 ); // '12' (string to the left)
90+
alert( 1 + '2' ); // '12' (eilutė iš dešinės)
91+
alert( '1' + 2 ); // '12' (eilutė iš kairės)
9292
```
9393
94-
This only happens when at least one of the arguments is a string. Otherwise, values are converted to numbers.
94+
Taip įvyksta tik tokiu atveju kai vienas iš argumentų yra eilutė. Kitu atveju vertės konvertuojamos į skaičius.
9595
````
9696

97-
## Boolean Conversion
97+
## Loginės konversijos
9898

99-
Boolean conversion is the simplest one.
99+
Loginės konversijos yra pačios paprasčiausios.
100100

101-
It happens in logical operations (later we'll meet condition tests and other similar things) but can also be performed explicitly with a call to `Boolean(value)`.
101+
Jos nutinka loginėse operacijose (vėliau dar matysime padėties testus, ang. condition tests, ir panašius atvejus), bet taip pat gali būti atliekamos akivaizdžiam `Boolean(value)` iškvietimui.
102102

103-
The conversion rule:
103+
Konversijos taisyklės:
104104

105-
- Values that are intuitively "empty", like `0`, an empty string, `null`, `undefined`, and `NaN`, become `false`.
106-
- Other values become `true`.
105+
- Vertės kurios intuityviai yra tuščios, kaip `0`, tuščia eilutė, `null`, `undefined` ir `NaN`, tampa `false`.
106+
- Kitos vertės tampa `true`.
107107

108-
For instance:
108+
Pavyzdžiui:
109109

110110
```js run
111111
alert( Boolean(1) ); // true
@@ -115,45 +115,45 @@ alert( Boolean("hello") ); // true
115115
alert( Boolean("") ); // false
116116
```
117117

118-
````warn header="Please note: the string with zero `\"0\"` is `true`"
119-
Some languages (namely PHP) treat `"0"` as `false`. But in JavaScript, a non-empty string is always `true`.
118+
````warn header="Atkreipkite dėmesė: eilutė su nuliu `\"0\"` yra `true`"
119+
Kai kurios kalbos (pavyzdžiui PHP) `"0"` laiko `false`. Bet JavaScript netuščia eilutė visada bus `true`.
120120

121121
```js run
122122
alert( Boolean("0") ); // true
123-
alert( Boolean(" ") ); // spaces, also true (any non-empty string is true)
123+
alert( Boolean(" ") ); // tarpai, taip pat tinka (bet kokia netuščia eilutė yra tinkama - true)
124124
```
125125
````
126126
127-
## Summary
127+
## Santrauka
128128
129-
The three most widely used type conversions are to string, to number, and to boolean.
129+
Trys labiausiai naudojamos tipų konversijos yra į eilutę, skaičių ir loginiai.
130130
131-
**`String Conversion`** -- Occurs when we output something. Can be performed with `String(value)`. The conversion to string is usually obvious for primitive values.
131+
**`Eilutės Konversija`** -- Nutinka kai mes kažką gauname. Gali būti atliekama su `String(value)`. Konversija į eilutę su primityviais tipais dažniausiai yra akivaizdi.
132132
133-
**`Numeric Conversion`** -- Occurs in math operations. Can be performed with `Number(value)`.
133+
**`Skaičių Konversija`** -- Nutinka matematinėse operacijos. Gali būti atliekama su `Number(value)`.
134134
135-
The conversion follows the rules:
135+
Konversija laikosi taisyklių:
136136
137-
| Value | Becomes... |
137+
| Vertė | Tampa... |
138138
|-------|-------------|
139139
|`undefined`|`NaN`|
140140
|`null`|`0`|
141141
|<code>true&nbsp;/&nbsp;false</code> | `1 / 0` |
142-
| `string` | The string is read "as is", whitespaces from both sides are ignored. An empty string becomes `0`. An error gives `NaN`. |
142+
| `string` | Eilutė skaitoma taip kaip yra, tarpai iš abiejų pusių ignoruojami. Tuščia eilutė tampa `0`. Klaida grąžina `NaN`. |
143143
144-
**`Boolean Conversion`** -- Occurs in logical operations. Can be performed with `Boolean(value)`.
144+
**`Loginės Konversijos`** -- Nutinka loginėse operacijose. Gali būti atliekama su `Boolean(value)`.
145145
146-
Follows the rules:
146+
Laikosi taisyklių:
147147
148-
| Value | Becomes... |
148+
| Vertė | Tampa... |
149149
|-------|-------------|
150150
|`0`, `null`, `undefined`, `NaN`, `""` |`false`|
151-
|any other value| `true` |
151+
|bet kokia kita vertė| `true` |
152152
153153
154-
Most of these rules are easy to understand and memorize. The notable exceptions where people usually make mistakes are:
154+
Didžiąją dalį šių taisyklių lengva suprasti ir prisiminti. Svarbios išimtys kur žmonės daro klaidas yra:
155155
156-
- `undefined` is `NaN` as a number, not `0`.
157-
- `"0"` and space-only strings like `" "` are true as a boolean.
156+
- `undefined` kaip skaičius yra `NaN` kaip skaičius, ne `0`.
157+
- `"0"` ir eilutė tik su tarpu kaip `" "` yra tiesa loginėse vertėse.
158158
159-
Objects aren't covered here. We'll return to them later in the chapter <info:object-toprimitive> that is devoted exclusively to objects after we learn more basic things about JavaScript.
159+
Objektai čia neaptariami. Prie jų sugrįšime vėliau skyriuje <info:object-toprimitive>, kuris yra skirtas išskirtinai objektams kai tik sužinosime daugiau apie JavaScript.

0 commit comments

Comments
 (0)