diff --git a/1-js/02-first-steps/08-comparison/1-comparison-questions/solution.md b/1-js/02-first-steps/08-comparison/1-comparison-questions/solution.md index 6437b512e..863b3d211 100644 --- a/1-js/02-first-steps/08-comparison/1-comparison-questions/solution.md +++ b/1-js/02-first-steps/08-comparison/1-comparison-questions/solution.md @@ -10,12 +10,12 @@ null == "\n0\n" → false null === +"\n0\n" → false ``` -Some of the reasons: +Kai kurios priežastys: -1. Obviously, true. -2. Dictionary comparison, hence false. -3. Again, dictionary comparison, first char of `"2"` is greater than the first char of `"1"`. -4. Values `null` and `undefined` equal each other only. -5. Strict equality is strict. Different types from both sides lead to false. -6. Similar to `(4)`, `null` only equals `undefined`. -7. Strict equality of different types. +1. Akivaizdžiai tiesa. +2. Žodynėlio palyginimas, tad netiesa. +3. Vėlgi, žodynėlio palyginimas, tad pirmasis ženklas eilutėje `"2"` yra didenis nei pirmasis ženklas eilutėje `"1"`. +4. Vertės `null` ir `undefined` yra lygios viena kitai. +5. Griežta lygybė yra griežta. Skirtingi tipai abiejose pusėse atveda prie netiesos. +6. Panašiai į `(4)`, `null` yra lygus tik `undefined`. +7. Griežta skirtingų tipų lygybė. diff --git a/1-js/02-first-steps/08-comparison/1-comparison-questions/task.md b/1-js/02-first-steps/08-comparison/1-comparison-questions/task.md index be7f75ddd..6ea4f38f1 100644 --- a/1-js/02-first-steps/08-comparison/1-comparison-questions/task.md +++ b/1-js/02-first-steps/08-comparison/1-comparison-questions/task.md @@ -2,9 +2,9 @@ importance: 5 --- -# Comparisons +# Palyginimai -What will be the result for these expressions? +Kokie bus šių išraiškų rezultatai? ```js no-beautify 5 > 4 diff --git a/1-js/02-first-steps/08-comparison/article.md b/1-js/02-first-steps/08-comparison/article.md index d889b1328..53a0ac3cd 100644 --- a/1-js/02-first-steps/08-comparison/article.md +++ b/1-js/02-first-steps/08-comparison/article.md @@ -1,41 +1,41 @@ -# Comparisons +# Palyginimai -We know many comparison operators from maths: +Iš matematikos mes žinome daug palyginimo operatorių: -- Greater/less than: a > b, a < b. -- Greater/less than or equals: a >= b, a <= b. -- Equals: `a == b` (please note the double equals sign `=`. A single symbol `a = b` would mean an assignment). -- Not equals. In maths the notation is , but in JavaScript it's written as an assignment with an exclamation sign before it: a != b. +- Daugiau/mažiau negu: a > b, a < b. +- Daugiau/mažiau arba lygu negu: a >= b, a <= b. +- Lygu: `a == b` (atkreipkite dėmesį į dvigubos lygybės ženklą `=`. Vienas ženklas `a = b` reikštų priskyrimą). +- Nelygus. Matematikoje toks ženklas yra , bet JavaScript jis rašomas kaip asigmentas su šauktuku prieš jį: a != b. -## Boolean is the result +## Loginė vertė yra rezultatas -Like all other operators, a comparison returns a value. In this case, the value is a boolean. +Kaip ir visi kiti operatoriai, palyginimas grąžina vertę. Šiuo atveju ta vertė yra loginė. -- `true` -- means "yes", "correct" or "the truth". -- `false` -- means "no", "wrong" or "not the truth". +- `true` -- reiškia "taip", "teisingai" arba "tiesa". +- `false` -- reiškia "ne", "neteisingai" arba "netiesa". -For example: +Pavyzdžiui: ```js run -alert( 2 > 1 ); // true (correct) -alert( 2 == 1 ); // false (wrong) -alert( 2 != 1 ); // true (correct) +alert( 2 > 1 ); // true (teisingai) +alert( 2 == 1 ); // false (neteisingai) +alert( 2 != 1 ); // true (teisingai) ``` -A comparison result can be assigned to a variable, just like any value: +Palyginimo rezultatas gali būti priskirtas kintamajam, kaip ir bet kuri kita vertė: ```js run -let result = 5 > 4; // assign the result of the comparison +let result = 5 > 4; // priskirti palyginimo rezultato vertę alert( result ); // true ``` -## String comparison +## Eilutės palyginimas -To see whether a string is greater than another, JavaScript uses the so-called "dictionary" or "lexicographical" order. +Kad patikrintų ar viena eilutė yra didesnė už kitą, JavaScript naudoja taip vadinamą "žodyno" arba "leksikografinį" eiliškumą -In other words, strings are compared letter-by-letter. +Kitais žodžiais, eilutės yra lyginamos paraidžiui. -For example: +Pavyzdžiui: ```js run alert( 'Z' > 'A' ); // true @@ -43,53 +43,53 @@ alert( 'Glow' > 'Glee' ); // true alert( 'Bee' > 'Be' ); // true ``` -The algorithm to compare two strings is simple: +Algoritmas eilučių palyginimui yra labai paprastas: -1. Compare the first character of both strings. -2. If the first character from the first string is greater (or less) than the other string's, then the first string is greater (or less) than the second. We're done. -3. Otherwise, if both strings' first characters are the same, compare the second characters the same way. -4. Repeat until the end of either string. -5. If both strings end at the same length, then they are equal. Otherwise, the longer string is greater. +1. Palyginti abiejų eilučių pirmus ženklus. +2. Jeigu pirmas ženklas iš pirmos eilutės yra didesnis (ar mažesnis) negu kitos eilutės, tada pirma eilutė yra didesnė (ar mažesnė) už antrąją. Pabaiga. +3. Kitu atveju, jeigu abiejų eilučių pirmi ženklai yra vienodi, tada lyginami antri ženklai tuo pačiu principu. +4. Pakartoti iki vienos iš eilučių pabaigos. +5. Jeigu abi eilutės baigiasi tuo pačiu metu, jos yra vienodos. Kitu atveju ilgesnė eilutė yra didesnė. -In the examples above, the comparison `'Z' > 'A'` gets to a result at the first step while the strings `"Glow"` and `"Glee"` are compared character-by-character: +Pavyzdyje aukščiau, palyginimas `'Z' > 'A'` gauna atsakymą pirmame žingsnyje, kai tuo tarpu `"Glow"` ir `"Glee"` yra lyginami ženklas po ženklo: -1. `G` is the same as `G`. -2. `l` is the same as `l`. -3. `o` is greater than `e`. Stop here. The first string is greater. +1. `G` tas pats kaip `G`. +2. `l` tas pats kaip `l`. +3. `o` yra didesnis nei `e`. Čia sustojame. Pirma eilutė yra didesnė. -```smart header="Not a real dictionary, but Unicode order" -The comparison algorithm given above is roughly equivalent to the one used in dictionaries or phone books, but it's not exactly the same. +```smart header="Ne tikras žodynas, bet Unicode eiliškumas" +Palyginimo algoritmas esantis aukščiau yra maždaug panašus į tokį koks naudojamas žodynuose ir telefonų knygoje, tačiau jis nėra visiškai toks pats. -For instance, case matters. A capital letter `"A"` is not equal to the lowercase `"a"`. Which one is greater? The lowercase `"a"`. Why? Because the lowercase character has a greater index in the internal encoding table JavaScript uses (Unicode). We'll get back to specific details and consequences of this in the chapter . +Pavyzdžiui svarbu ar raidės yra mažosios ar didžiosios. Didžioji raidė `"A"` nėra lygi mažajai raidei `"a"`. Kuri yra didesnė? Mažoji `"a"`. Kodėl? Nes mažosios raidės turi aukštesnį indeksą vidinėje JavaScript kodavimo lentelėje (Unicode). Mes sugrįšime prie specifinių detalių ir pasekmių skyriuje . ``` -## Comparison of different types +## Skirtingų tipų palyginimai -When comparing values of different types, JavaScript converts the values to numbers. +JavaScript lygindama skirtingų tipų vertes, jas paverčia į skaičius. -For example: +Pavyzdžiui: ```js run -alert( '2' > 1 ); // true, string '2' becomes a number 2 -alert( '01' == 1 ); // true, string '01' becomes a number 1 +alert( '2' > 1 ); // true, eilutė '2' tampa skaičiumi 2 +alert( '01' == 1 ); // true, eilutė '01' tampa skaičiumi 1 ``` -For boolean values, `true` becomes `1` and `false` becomes `0`. +Loginėse vertėse, `true` tampa `1`, o `false` tampa `0`. -For example: +Pavyzdžiui: ```js run alert( true == 1 ); // true alert( false == 0 ); // true ``` -````smart header="A funny consequence" -It is possible that at the same time: +````smart header="Linksmas sutapimas" +Ar įmanoma, kad tuo pačiu metu: -- Two values are equal. -- One of them is `true` as a boolean and the other one is `false` as a boolean. +- Dvi vertės yra vienodos. +- Kaip loginė vertė viena iš jų yra `true`, o kita yra `false`. -For example: +Pavyzdžiui: ```js run let a = 0; @@ -101,67 +101,67 @@ alert( Boolean(b) ); // true alert(a == b); // true! ``` -From JavaScript's standpoint, this result is quite normal. An equality check converts values using the numeric conversion (hence `"0"` becomes `0`), while the explicit `Boolean` conversion uses another set of rules. +Iš JavaScript pozicijos, toks rezultatas yra visiškai normalus. Palyginimas paverčia vertes naudodamas skaičių konversijas (tad `"0"` tampa `0`), o tuo tarpu išskirtinė loginė `Boolean` konversija naudoja kitokias taisykles. ```` -## Strict equality +## Griežta lygybė -A regular equality check `==` has a problem. It cannot differentiate `0` from `false`: +Įprastinės lygybės patikrinimas `==` turi problemą. Ji negali atskirti `0` nuo `false`: ```js run alert( 0 == false ); // true ``` -The same thing happens with an empty string: +Tas pats nutinka su tuščia eilutė: ```js run alert( '' == false ); // true ``` -This happens because operands of different types are converted to numbers by the equality operator `==`. An empty string, just like `false`, becomes a zero. +Taip nutinka dėl to, kad skirtingų tipų operandai naudojant lygybės operatorių `==` yra paverčiami į skaičius. Tuščia eilutė, taip pat kaip ir `false`, tampa nuliu. -What to do if we'd like to differentiate `0` from `false`? +Ką daryti jeigu mes norime, kad `0` skirtųsi nuo `false`? -**A strict equality operator `===` checks the equality without type conversion.** +**Griežtos lygybės operatorius `===` patikrina lygybę nedarydamas tipo konversijos.** -In other words, if `a` and `b` are of different types, then `a === b` immediately returns `false` without an attempt to convert them. +Kitaip sakant, jeigu `a` ir `b` yra skirtingų tipų, tada `a === b` iš karto grąžina `false` net nebandydama jų konvertuoti. -Let's try it: +Pabandykime: ```js run -alert( 0 === false ); // false, because the types are different +alert( 0 === false ); // false, nes tipai yra skirtingi ``` -There is also a "strict non-equality" operator `!==` analogous to `!=`. +Taip pat yra ir "griežtos nelygybės" operatorius `!==` analogiškas `!=`. -The strict equality operator is a bit longer to write, but makes it obvious what's going on and leaves less room for errors. +Griežtos lygybės operatorius yra ilgesnis, bet jis padeda kodui atrodyti aiškesniu ir palieka mažiau vietos klaidoms. -## Comparison with null and undefined +## Palyginimai su null ir undefined -There's a non-intuitive behavior when `null` or `undefined` are compared to other values. +Kai lyginami`null` ar `undefined`su kitomis vertėmis elgesys nėra intuityvus. -For a strict equality check `===` -: These values are different, because each of them is a different type. +Griežtos lygybės patikrinime `===` +: Šios vertės yra skirtingos, nes kiekviena jų yra skirtingo tipo. ```js run alert( null === undefined ); // false ``` -For a non-strict check `==` -: There's a special rule. These two are a "sweet couple": they equal each other (in the sense of `==`), but not any other value. +Negriežtos lygybės patikrai `==` +: Yra speciali taisyklė. Šie du yra "saldi porelė": jie yra lygūs vienas kitam (kai yra `==`), bet jokioms kitoms vertėms. ```js run alert( null == undefined ); // true ``` -For maths and other comparisons `< > <= >=` -: `null/undefined` are converted to numbers: `null` becomes `0`, while `undefined` becomes `NaN`. +Matematiniuose ir kitokiuose palyginimuose `< > <= >=` +: `null/undefined` yra paverčiami skaičiais: `null` tampa `0`, tuo tarpu `undefined` tampa `NaN`. -Now let's see some funny things that happen when we apply these rules. And, what's more important, how to not fall into a trap with them. +Dabar pasižiūrėkime į juokingus atvejus kai šios taisyklės būna pritaikomos. Ir visų svarbiausia kaip dėl jų nepakliūti į spąstus. -### Strange result: null vs 0 +### Keistas rezultatas: null vs 0 -Let's compare `null` with a zero: +Palyginkime `null` su nuliu: ```js run alert( null > 0 ); // (1) false @@ -169,15 +169,15 @@ alert( null == 0 ); // (2) false alert( null >= 0 ); // (3) *!*true*/!* ``` -Mathematically, that's strange. The last result states that "`null` is greater than or equal to zero", so in one of the comparisons above it must be `true`, but they are both false. +Tai labai keista matematiškai. Rezultatas sako, kad "`null` yra didesnis arba lygus nuliu", vadinasi viename iš aukščiau esančių palyginimų turėtų būti taip pat `true`, bet jie abu yra neteisingi. -The reason is that an equality check `==` and comparisons `> < >= <=` work differently. Comparisons convert `null` to a number, treating it as `0`. That's why (3) `null >= 0` is true and (1) `null > 0` is false. +To priežastis yra tai, kad lygybės patikrinimas `==` ir palyginimai `> < >= <=` veikia kitaip. Palyginimai paverčia `null` į skaičių ir laiko jį nuliu `0`. Štai dėl ko (3) `null >= 0` yra tiesa, o `null > 0` yra netiesa. -On the other hand, the equality check `==` for `undefined` and `null` is defined such that, without any conversions, they equal each other and don't equal anything else. That's why (2) `null == 0` is false. +Iš kitos pusės, lygybės patikrinimas `==` kaip jau yra apibūdinta, `undefined` ir `null` kai jie nėra konvertuojami, jie yra vienas kitam lygūs, bet nelygūs niekam kitam. Štai kodėl (2) `null == 0` yra netiesa. -### An incomparable undefined +### Nepalyginimasis undefined -The value `undefined` shouldn't be compared to other values: +Vertė `undefined` neturėtų būti lyginima su kitomis vertėmis: ```js run alert( undefined > 0 ); // false (1) @@ -185,25 +185,25 @@ alert( undefined < 0 ); // false (2) alert( undefined == 0 ); // false (3) ``` -Why does it dislike zero so much? Always false! +Kodėl jis taip nemėgsta nulio? Visada netiesa! -We get these results because: +Gauname tokius rezultatas, nes: -- Comparisons `(1)` and `(2)` return `false` because `undefined` gets converted to `NaN` and `NaN` is a special numeric value which returns `false` for all comparisons. -- The equality check `(3)` returns `false` because `undefined` only equals `null`, `undefined`, and no other value. +- Palyginimai `(1)` ir `(2)` grąžina `false`, nes `undefined` paverčiamas į `NaN`, o `NaN` yra ypatinga skaitinė vertė, kuri visoms vertėms grąžina `false`. +- Lygybės patikrinimas `(3)` grąžina `false`, nes `undefined` yra lygus tik `null`, `undefined` ir jokiai kitai vertei. -### Evade problems +### Išvenkite problemų -Why did we go over these examples? Should we remember these peculiarities all the time? Well, not really. Actually, these tricky things will gradually become familiar over time, but there's a solid way to evade problems with them: +Kodėl mes peržiūrėjome tokius pavyzdžius? Ar turėtume tokias keistenybes visada prisiminti? Nebūtinai. Tiesą sakant tokie triukai taps pažįstamais su laiku, bet yra būdas kaip išvengti su jais susijusių problemų: -Just treat any comparison with `undefined/null` except the strict equality `===` with exceptional care. +Kiekvieną palyginimą susijusį su `undefined/null` vertinkite atsargiai, išskyrus su griežta lygybe `===`. -Don't use comparisons `>= > < <=` with a variable which may be `null/undefined`, unless you're really sure of what you're doing. If a variable can have these values, check for them separately. +Nenaudokite palyginimų `>= > < <=` su kintamuoju, kuris gali būti `null/undefined`, nebent tikrai žinote ką darote. Jeigu kintamasis gali turėti tokias vertybes, patikrinkite jas atskirai. -## Summary +## Santrauka -- Comparison operators return a boolean value. -- Strings are compared letter-by-letter in the "dictionary" order. -- When values of different types are compared, they get converted to numbers (with the exclusion of a strict equality check). -- The values `null` and `undefined` equal `==` each other and do not equal any other value. -- Be careful when using comparisons like `>` or `<` with variables that can occasionally be `null/undefined`. Checking for `null/undefined` separately is a good idea. +- Palyginimų operatoriai grąžina loginę vertę. +- Eilutės yra tikrinamos paraidžiui "žodynėlio" eiliškumo principu. +- Kai lyginamos skirtingų tipų vertės, jos yra paverčiamos į skaičius (išskyrus tik griežtą lygybės patikrinimą). +- Vertės `null` ir `undefined` yra lygios `==` viena kitai, bet nelygios jokiai kitai vertei. +- Būkite atsargūs kai naudojate tokius palyginimus kaip `>` arba `<` su kintamaisiai, kurie laikas nuo laiko gali būti `null/undefined`. Gera mintis yra patikrinti `null/undefined` atskirai.