From f7cd1b223ddf2e3a7ce6801b71409dc7ce686d54 Mon Sep 17 00:00:00 2001 From: FarisPalayi Date: Fri, 21 May 2021 18:17:34 +0530 Subject: [PATCH 01/16] =?UTF-8?q?Comparisons=20Summary=20section=20malayal?= =?UTF-8?q?am=20translation=20completed=20=E2=9C=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../09-comparison/article-en.md | 216 +++++++++++++++++ 1-js/02-first-steps/09-comparison/article.md | 220 +----------------- 2 files changed, 222 insertions(+), 214 deletions(-) create mode 100644 1-js/02-first-steps/09-comparison/article-en.md diff --git a/1-js/02-first-steps/09-comparison/article-en.md b/1-js/02-first-steps/09-comparison/article-en.md new file mode 100644 index 000000000..ead7922ff --- /dev/null +++ b/1-js/02-first-steps/09-comparison/article-en.md @@ -0,0 +1,216 @@ +# Comparisons + +We know many comparison operators from maths. + +In JavaScript they are written like this: + +- Greater/less than: a > b, a < b. +- Greater/less than or equals: a >= b, a <= b. +- Equals: `a == b`, please note the double equality sign `==` means the equality test, while a single one `a = b` means an assignment. +- Not equals. In maths the notation is , but in JavaScript it's written as a != b. + +In this article we'll learn more about different types of comparisons, how JavaScript makes them, including important peculiarities. + +At the end you'll find a good recipe to avoid "JavaScript quirks"-related issues. + +## Boolean is the result + +All comparison operators return a boolean value: + +- `true` -- means "yes", "correct" or "the truth". +- `false` -- means "no", "wrong" or "not the truth". + +For example: + +```js run +alert( 2 > 1 ); // true (correct) +alert( 2 == 1 ); // false (wrong) +alert( 2 != 1 ); // true (correct) +``` + +A comparison result can be assigned to a variable, just like any value: + +```js run +let result = 5 > 4; // assign the result of the comparison +alert( result ); // true +``` + +## String comparison + +To see whether a string is greater than another, JavaScript uses the so-called "dictionary" or "lexicographical" order. + +In other words, strings are compared letter-by-letter. + +For example: + +```js run +alert( 'Z' > 'A' ); // true +alert( 'Glow' > 'Glee' ); // true +alert( 'Bee' > 'Be' ); // true +``` + +The algorithm to compare two strings is simple: + +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. + +In the first example above, the comparison `'Z' > 'A'` gets to a result at the first step. + +The second comparison `'Glow'` and `'Glee'` needs more steps as strings are compared character-by-character: + +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. + +```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. + +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 . +``` + +## Comparison of different types + +When comparing values of different types, JavaScript converts the values to numbers. + +For example: + +```js run +alert( '2' > 1 ); // true, string '2' becomes a number 2 +alert( '01' == 1 ); // true, string '01' becomes a number 1 +``` + +For boolean values, `true` becomes `1` and `false` becomes `0`. + +For example: + +```js run +alert( true == 1 ); // true +alert( false == 0 ); // true +``` + +````smart header="A funny consequence" +It is possible that at the same time: + +- Two values are equal. +- One of them is `true` as a boolean and the other one is `false` as a boolean. + +For example: + +```js run +let a = 0; +alert( Boolean(a) ); // false + +let b = "0"; +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. +```` + +## Strict equality + +A regular equality check `==` has a problem. It cannot differentiate `0` from `false`: + +```js run +alert( 0 == false ); // true +``` + +The same thing happens with an empty string: + +```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. + +What to do if we'd like to differentiate `0` from `false`? + +**A strict equality operator `===` checks the equality without type conversion.** + +In other words, if `a` and `b` are of different types, then `a === b` immediately returns `false` without an attempt to convert them. + +Let's try it: + +```js run +alert( 0 === false ); // false, because the types are different +``` + +There is also a "strict non-equality" operator `!==` analogous to `!=`. + +The strict equality operator is a bit longer to write, but makes it obvious what's going on and leaves less room for errors. + +## Comparison with null and undefined + +There's a non-intuitive behavior when `null` or `undefined` are compared to other values. + +For a strict equality check `===` +: These values are different, because each of them is a different type. + + ```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. + + ```js run + alert( null == undefined ); // true + ``` + +For maths and other comparisons `< > <= >=` +: `null/undefined` are converted to numbers: `null` becomes `0`, while `undefined` becomes `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. + +### Strange result: null vs 0 + +Let's compare `null` with a zero: + +```js run +alert( null > 0 ); // (1) false +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. + +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. + +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. + +### An incomparable undefined + +The value `undefined` shouldn't be compared to other values: + +```js run +alert( undefined > 0 ); // false (1) +alert( undefined < 0 ); // false (2) +alert( undefined == 0 ); // false (3) +``` + +Why does it dislike zero so much? Always false! + +We get these results because: + +- 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. + +### Avoid problems + +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 avoid problems with them: + +- Treat any comparison with `undefined/null` except the strict equality `===` with exceptional care. +- 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. + +## Summary + +- 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. diff --git a/1-js/02-first-steps/09-comparison/article.md b/1-js/02-first-steps/09-comparison/article.md index ead7922ff..9886f0134 100644 --- a/1-js/02-first-steps/09-comparison/article.md +++ b/1-js/02-first-steps/09-comparison/article.md @@ -1,216 +1,8 @@ -# Comparisons -We know many comparison operators from maths. +## സംഗ്രഹം -In JavaScript they are written like this: - -- Greater/less than: a > b, a < b. -- Greater/less than or equals: a >= b, a <= b. -- Equals: `a == b`, please note the double equality sign `==` means the equality test, while a single one `a = b` means an assignment. -- Not equals. In maths the notation is , but in JavaScript it's written as a != b. - -In this article we'll learn more about different types of comparisons, how JavaScript makes them, including important peculiarities. - -At the end you'll find a good recipe to avoid "JavaScript quirks"-related issues. - -## Boolean is the result - -All comparison operators return a boolean value: - -- `true` -- means "yes", "correct" or "the truth". -- `false` -- means "no", "wrong" or "not the truth". - -For example: - -```js run -alert( 2 > 1 ); // true (correct) -alert( 2 == 1 ); // false (wrong) -alert( 2 != 1 ); // true (correct) -``` - -A comparison result can be assigned to a variable, just like any value: - -```js run -let result = 5 > 4; // assign the result of the comparison -alert( result ); // true -``` - -## String comparison - -To see whether a string is greater than another, JavaScript uses the so-called "dictionary" or "lexicographical" order. - -In other words, strings are compared letter-by-letter. - -For example: - -```js run -alert( 'Z' > 'A' ); // true -alert( 'Glow' > 'Glee' ); // true -alert( 'Bee' > 'Be' ); // true -``` - -The algorithm to compare two strings is simple: - -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. - -In the first example above, the comparison `'Z' > 'A'` gets to a result at the first step. - -The second comparison `'Glow'` and `'Glee'` needs more steps as strings are compared character-by-character: - -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. - -```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. - -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 . -``` - -## Comparison of different types - -When comparing values of different types, JavaScript converts the values to numbers. - -For example: - -```js run -alert( '2' > 1 ); // true, string '2' becomes a number 2 -alert( '01' == 1 ); // true, string '01' becomes a number 1 -``` - -For boolean values, `true` becomes `1` and `false` becomes `0`. - -For example: - -```js run -alert( true == 1 ); // true -alert( false == 0 ); // true -``` - -````smart header="A funny consequence" -It is possible that at the same time: - -- Two values are equal. -- One of them is `true` as a boolean and the other one is `false` as a boolean. - -For example: - -```js run -let a = 0; -alert( Boolean(a) ); // false - -let b = "0"; -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. -```` - -## Strict equality - -A regular equality check `==` has a problem. It cannot differentiate `0` from `false`: - -```js run -alert( 0 == false ); // true -``` - -The same thing happens with an empty string: - -```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. - -What to do if we'd like to differentiate `0` from `false`? - -**A strict equality operator `===` checks the equality without type conversion.** - -In other words, if `a` and `b` are of different types, then `a === b` immediately returns `false` without an attempt to convert them. - -Let's try it: - -```js run -alert( 0 === false ); // false, because the types are different -``` - -There is also a "strict non-equality" operator `!==` analogous to `!=`. - -The strict equality operator is a bit longer to write, but makes it obvious what's going on and leaves less room for errors. - -## Comparison with null and undefined - -There's a non-intuitive behavior when `null` or `undefined` are compared to other values. - -For a strict equality check `===` -: These values are different, because each of them is a different type. - - ```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. - - ```js run - alert( null == undefined ); // true - ``` - -For maths and other comparisons `< > <= >=` -: `null/undefined` are converted to numbers: `null` becomes `0`, while `undefined` becomes `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. - -### Strange result: null vs 0 - -Let's compare `null` with a zero: - -```js run -alert( null > 0 ); // (1) false -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. - -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. - -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. - -### An incomparable undefined - -The value `undefined` shouldn't be compared to other values: - -```js run -alert( undefined > 0 ); // false (1) -alert( undefined < 0 ); // false (2) -alert( undefined == 0 ); // false (3) -``` - -Why does it dislike zero so much? Always false! - -We get these results because: - -- 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. - -### Avoid problems - -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 avoid problems with them: - -- Treat any comparison with `undefined/null` except the strict equality `===` with exceptional care. -- 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. - -## Summary - -- 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. +- താരതമ്യ ഓപറേറ്ററുകൾ ഒരു ബൂലീൻ വാല്യു (`true` or `false`) പുറത്ത് വിടുന്നു. +- സ്ട്രിങ്ങുകൾ ഓരോ അക്ഷരം വീതമാണ് താരതമ്യം ചെയ്യപ്പെടുന്നത്. ഒരു ഡിക്ഷ്ണറിയിൽ വാക്കുകൾ ക്രമീകരിച്ചിരിക്കുന്നത് പോലെ. +- വ്യത്യസ്ത വിഭാഗങ്ങളിൽപ്പെട്ട വാല്യൂകൾ തമ്മിൽ താരതമ്യം ചെയ്യുമ്പോൾ, അവ ആദ്യം ഒരു നമ്പറിലേക്ക് മാറ്റപ്പെടുന്നു.( സ്ട്രിക്റ്റ് ഇക്വാലിറ്റി ഓപറേറ്ററുകൾ(`===` and `!==`) ഉപയോഗിക്കുമ്പേൾ ഇത് ബാധകമാവുന്നില്ല). +- `null`,`undefined` വാല്യുകൾ അവയെ പരസ്പരം പൂർണമാക്കുന്നു(`==`). അവ മറ്റൊരു വാല്യുവുമായും തുല്യമാകുന്നില്ല. +- `null/undefined` വാല്ല്യു ഉണ്ടാവാൻ സാധ്യതയുള്ള വേരിയബ്ൾസുമായി `>` അല്ലെങ്കിൽ `<` പോലെയുള്ള താരതമ്യ ഓപറേറ്ററുകൾ ഉപയോഗിച്ച് താരതമ്യം ചെയ്യുമ്പോൾ സൂക്ഷിക്കുക. `null/undefined` വാല്ല്യുകളെ വെവ്വേറെ താരതമ്യം ചെയ്യുന്നതാവും ഉത്തമം. \ No newline at end of file From 77d75c18cc7d3838f7265034592a0ed6b9877ab9 Mon Sep 17 00:00:00 2001 From: FarisPalayi Date: Fri, 21 May 2021 20:31:15 +0530 Subject: [PATCH 02/16] Comparisons intro section malayalam translation completed --- 1-js/02-first-steps/09-comparison/article.md | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/1-js/02-first-steps/09-comparison/article.md b/1-js/02-first-steps/09-comparison/article.md index 9886f0134..5a33c702e 100644 --- a/1-js/02-first-steps/09-comparison/article.md +++ b/1-js/02-first-steps/09-comparison/article.md @@ -1,8 +1,21 @@ +# താരതമ്യപ്പെടുത്തൽ + +ഗണിതത്തിൽ സംഖ്യകളെ പരസ്പരം താരതമ്യം ചെയ്യുന്നത് എങ്ങനെയെന്ന് നമുക്കറിയാം. + +ജാവാസ്ക്രിപ്റ്റിൽ അത് ഇങ്ങനെയാണ് : +- ഒന്ന് മറ്റൊന്നിനെക്കാൾ വലുത്/ചെറുത് : അ > ആ, അ < ആ (a > b, a < b). +- ഒന്ന് മറ്റൊന്നിനെക്കാൾ വലുത്/ചെറുത് അല്ലെങ്കിൽ തുല്ല്യം : അ >= ആ, അ <= ആ (a >= b, a <= b). +- തുല്ല്യം/സമം : `അ === ആ`. ശ്രദ്ധിക്കുക രണ്ട് സമചിഹ്നം (`അ == ആ`) തുല്ല്യമാണോ എന്ന് പരിശോധിക്കാൻ ഉപയോഗിക്കുന്നു. എന്നാൽ ഒരു സമചിഹ്നം (`അ = ആ`) കൊണ്ടുദ്ദേശിക്കുന്നത് 'വാല്ല്യു കൊടുക്കുക (assignment)' എന്നാണ്. +- തുല്ല്യമല്ല : ഗണിതത്തിൽ ഇതിനുപയോഗിക്കുന്നത് ചിഹ്നമാണ്, എന്നാൽ ജാവാസ്ക്രിപ്റ്റിൽ അതിങ്ങനെയാണ് എഴുതുന്നത്: അ != ആ (a != b). + +ഈ ലേഖനത്തിൽ പലതരം താരതമ്യപ്പെടുത്തലുകളെക്കുറിച്ചും, ജാവാസ്ക്രിപ്റ്റ് അവയെ എങ്ങനെയാണ് നിർമിക്കുന്നതെന്നും, നമ്മൾ പഠിക്കും. ജാവാസ്ക്രിപ്റ്റിൻ്റെ ചില പ്രധാനപ്പെട്ട 'അസ്വാഭാവിക പെരുമാറ്റങ്ങൾ' ഉൾപ്പെടെ. + +ഇതിൻ്റെ അവസാനം ജാവാസ്ക്രിപ്റ്റിൻ്റെ ഇത്തരം അസ്വാഭാവിക പെരുമാറ്റങ്ങൾ കൊണ്ടുണ്ടാവുന്ന പ്രശ്നങ്ങളെ എങ്ങനെ ഒഴിവാക്കാം എന്നതിനെപ്പറ്റി നമുക്ക് നല്ല ധാരണ ലഭിക്കും. ## സംഗ്രഹം - താരതമ്യ ഓപറേറ്ററുകൾ ഒരു ബൂലീൻ വാല്യു (`true` or `false`) പുറത്ത് വിടുന്നു. - സ്ട്രിങ്ങുകൾ ഓരോ അക്ഷരം വീതമാണ് താരതമ്യം ചെയ്യപ്പെടുന്നത്. ഒരു ഡിക്ഷ്ണറിയിൽ വാക്കുകൾ ക്രമീകരിച്ചിരിക്കുന്നത് പോലെ. -- വ്യത്യസ്ത വിഭാഗങ്ങളിൽപ്പെട്ട വാല്യൂകൾ തമ്മിൽ താരതമ്യം ചെയ്യുമ്പോൾ, അവ ആദ്യം ഒരു നമ്പറിലേക്ക് മാറ്റപ്പെടുന്നു.( സ്ട്രിക്റ്റ് ഇക്വാലിറ്റി ഓപറേറ്ററുകൾ(`===` and `!==`) ഉപയോഗിക്കുമ്പേൾ ഇത് ബാധകമാവുന്നില്ല). +- വ്യത്യസ്ത വിഭാഗങ്ങളിൽപ്പെട്ട വാല്യൂകൾ തമ്മിൽ താരതമ്യം ചെയ്യുമ്പോൾ, അവ ആദ്യം ഒരു നമ്പറിലേക്ക് മാറ്റപ്പെടുന്നു.( സ്ട്രിക്റ്റ് ഇക്വാലിറ്റി ഓപറേറ്ററുകൾ(`===` and `!==`) ഉപയോഗിക്കുമ്പേൾ ഇത് ബാധകമാവുന്നില്ല ). - `null`,`undefined` വാല്യുകൾ അവയെ പരസ്പരം പൂർണമാക്കുന്നു(`==`). അവ മറ്റൊരു വാല്യുവുമായും തുല്യമാകുന്നില്ല. -- `null/undefined` വാല്ല്യു ഉണ്ടാവാൻ സാധ്യതയുള്ള വേരിയബ്ൾസുമായി `>` അല്ലെങ്കിൽ `<` പോലെയുള്ള താരതമ്യ ഓപറേറ്ററുകൾ ഉപയോഗിച്ച് താരതമ്യം ചെയ്യുമ്പോൾ സൂക്ഷിക്കുക. `null/undefined` വാല്ല്യുകളെ വെവ്വേറെ താരതമ്യം ചെയ്യുന്നതാവും ഉത്തമം. \ No newline at end of file +- `null/undefined` വാല്ല്യു ഉണ്ടാവാൻ സാധ്യതയുള്ള വേരിയബ്ൾസുമായി `>` അല്ലെങ്കിൽ `<` പോലെയുള്ള താരതമ്യ ഓപറേറ്ററുകൾ ഉപയോഗിച്ച് താരതമ്യം ചെയ്യുമ്പോൾ ശ്രദ്ധിക്കുക. `null/undefined` വാല്ല്യുകളെ വെവ്വേറെ താരതമ്യം ചെയ്യുന്നതാവും ഉത്തമം. \ No newline at end of file From 1c061e1b1d0070035c08ef276240edd8591b69e3 Mon Sep 17 00:00:00 2001 From: FarisPalayi Date: Fri, 21 May 2021 21:18:17 +0530 Subject: [PATCH 03/16] =?UTF-8?q?Comparisons=20"Boolean=20is=20the=20resul?= =?UTF-8?q?t"=20section=20malayalam=20translation=20completed=20=E2=9C=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1-js/02-first-steps/09-comparison/article.md | 26 ++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/1-js/02-first-steps/09-comparison/article.md b/1-js/02-first-steps/09-comparison/article.md index 5a33c702e..6fde421fe 100644 --- a/1-js/02-first-steps/09-comparison/article.md +++ b/1-js/02-first-steps/09-comparison/article.md @@ -12,10 +12,32 @@ ഇതിൻ്റെ അവസാനം ജാവാസ്ക്രിപ്റ്റിൻ്റെ ഇത്തരം അസ്വാഭാവിക പെരുമാറ്റങ്ങൾ കൊണ്ടുണ്ടാവുന്ന പ്രശ്നങ്ങളെ എങ്ങനെ ഒഴിവാക്കാം എന്നതിനെപ്പറ്റി നമുക്ക് നല്ല ധാരണ ലഭിക്കും. +## റിസൾട്ടായി ലഭിക്കുന്നത് ബൂലീൻ ആയിരിക്കും + +എല്ലാ താരതമ്യ ഓപറേറ്ററുകളും ഒരു ബൂലീൻ വാല്ല്യുവാണ് റിട്ടേൺ ചെയ്യുന്നത്: + +- `true` -- അർത്ഥം "അതെ", "ശരി" അല്ലെങ്കിൽ "സത്യം". +- `false` -- അർത്ഥം "അല്ല", "തെറ്റ്", അല്ലെങ്കിൽ "സത്യമല്ല". + +ഉദാഹരണത്തിന്: + +```js run +alert( 2 > 1 ); // true (ശരി) +alert( 2 == 1 ); // false (തെറ്റ്) +alert( 2 != 1 ); // true (ശരി) +``` + +മറ്റേതൊരു വാല്ല്യുവും പോലെ താരതമ്യം ചെയ്ത് കിട്ടുന്ന റിസൾട്ടിനെ ഒരു വേരിയബിളിൽ കൊടുക്കാൻ(assign ചെയ്യാൻ) സാധിക്കും. + +```js run +let റിസൾട്ട് = 5 > 4; // റിസൾട്ടിനെ 'റിസൾട്ട്' എന്ന വേരിയബിളിൽ സുക്ഷിക്കുന്നു (assign ചെയ്യുന്നു) +alert(റിസൾട്ട്); // true +``` + ## സംഗ്രഹം -- താരതമ്യ ഓപറേറ്ററുകൾ ഒരു ബൂലീൻ വാല്യു (`true` or `false`) പുറത്ത് വിടുന്നു. +- താരതമ്യ ഓപറേറ്ററുകൾ ഒരു ബൂലീൻ വാല്യു (`true` or `false`) പുറത്ത് വിടുന്നു (റിട്ടേൺ ചെയ്യുന്നു). - സ്ട്രിങ്ങുകൾ ഓരോ അക്ഷരം വീതമാണ് താരതമ്യം ചെയ്യപ്പെടുന്നത്. ഒരു ഡിക്ഷ്ണറിയിൽ വാക്കുകൾ ക്രമീകരിച്ചിരിക്കുന്നത് പോലെ. - വ്യത്യസ്ത വിഭാഗങ്ങളിൽപ്പെട്ട വാല്യൂകൾ തമ്മിൽ താരതമ്യം ചെയ്യുമ്പോൾ, അവ ആദ്യം ഒരു നമ്പറിലേക്ക് മാറ്റപ്പെടുന്നു.( സ്ട്രിക്റ്റ് ഇക്വാലിറ്റി ഓപറേറ്ററുകൾ(`===` and `!==`) ഉപയോഗിക്കുമ്പേൾ ഇത് ബാധകമാവുന്നില്ല ). - `null`,`undefined` വാല്യുകൾ അവയെ പരസ്പരം പൂർണമാക്കുന്നു(`==`). അവ മറ്റൊരു വാല്യുവുമായും തുല്യമാകുന്നില്ല. -- `null/undefined` വാല്ല്യു ഉണ്ടാവാൻ സാധ്യതയുള്ള വേരിയബ്ൾസുമായി `>` അല്ലെങ്കിൽ `<` പോലെയുള്ള താരതമ്യ ഓപറേറ്ററുകൾ ഉപയോഗിച്ച് താരതമ്യം ചെയ്യുമ്പോൾ ശ്രദ്ധിക്കുക. `null/undefined` വാല്ല്യുകളെ വെവ്വേറെ താരതമ്യം ചെയ്യുന്നതാവും ഉത്തമം. \ No newline at end of file +- `null/undefined` വാല്ല്യു ലഭിക്കാൻ സാധ്യതയുള്ള വേരിയബ്ൾസുമായി `>` അല്ലെങ്കിൽ `<` പോലെയുള്ള താരതമ്യ ഓപറേറ്ററുകൾ ഉപയോഗിച്ച് താരതമ്യം ചെയ്യുമ്പോൾ ശ്രദ്ധിക്കുക. `null/undefined` വാല്ല്യുകളെ വെവ്വേറെ താരതമ്യം ചെയ്യുന്നതാവും ഉത്തമം. \ No newline at end of file From 031dad1215cadaec5977bd819edf9ded2b3c96ce Mon Sep 17 00:00:00 2001 From: FarisPalayi Date: Fri, 21 May 2021 23:24:49 +0530 Subject: [PATCH 04/16] =?UTF-8?q?Comparisons=20'comparing=20strings'=20sec?= =?UTF-8?q?tion=20malayalam=20translation=20completed=20=E2=9C=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1-js/02-first-steps/09-comparison/article.md | 49 ++++++++++++++++++-- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/1-js/02-first-steps/09-comparison/article.md b/1-js/02-first-steps/09-comparison/article.md index 6fde421fe..5e51481c6 100644 --- a/1-js/02-first-steps/09-comparison/article.md +++ b/1-js/02-first-steps/09-comparison/article.md @@ -3,6 +3,7 @@ ഗണിതത്തിൽ സംഖ്യകളെ പരസ്പരം താരതമ്യം ചെയ്യുന്നത് എങ്ങനെയെന്ന് നമുക്കറിയാം. ജാവാസ്ക്രിപ്റ്റിൽ അത് ഇങ്ങനെയാണ് : + - ഒന്ന് മറ്റൊന്നിനെക്കാൾ വലുത്/ചെറുത് : അ > ആ, അ < ആ (a > b, a < b). - ഒന്ന് മറ്റൊന്നിനെക്കാൾ വലുത്/ചെറുത് അല്ലെങ്കിൽ തുല്ല്യം : അ >= ആ, അ <= ആ (a >= b, a <= b). - തുല്ല്യം/സമം : `അ === ആ`. ശ്രദ്ധിക്കുക രണ്ട് സമചിഹ്നം (`അ == ആ`) തുല്ല്യമാണോ എന്ന് പരിശോധിക്കാൻ ഉപയോഗിക്കുന്നു. എന്നാൽ ഒരു സമചിഹ്നം (`അ = ആ`) കൊണ്ടുദ്ദേശിക്കുന്നത് 'വാല്ല്യു കൊടുക്കുക (assignment)' എന്നാണ്. @@ -10,7 +11,7 @@ ഈ ലേഖനത്തിൽ പലതരം താരതമ്യപ്പെടുത്തലുകളെക്കുറിച്ചും, ജാവാസ്ക്രിപ്റ്റ് അവയെ എങ്ങനെയാണ് നിർമിക്കുന്നതെന്നും, നമ്മൾ പഠിക്കും. ജാവാസ്ക്രിപ്റ്റിൻ്റെ ചില പ്രധാനപ്പെട്ട 'അസ്വാഭാവിക പെരുമാറ്റങ്ങൾ' ഉൾപ്പെടെ. -ഇതിൻ്റെ അവസാനം ജാവാസ്ക്രിപ്റ്റിൻ്റെ ഇത്തരം അസ്വാഭാവിക പെരുമാറ്റങ്ങൾ കൊണ്ടുണ്ടാവുന്ന പ്രശ്നങ്ങളെ എങ്ങനെ ഒഴിവാക്കാം എന്നതിനെപ്പറ്റി നമുക്ക് നല്ല ധാരണ ലഭിക്കും. +ഇതിൻ്റെ അവസാനം ജാവാസ്ക്രിപ്റ്റിൻ്റെ ഇത്തരം അസ്വാഭാവിക പെരുമാറ്റങ്ങൾ കൊണ്ടുണ്ടാവുന്ന പ്രശ്നങ്ങളെ എങ്ങനെ ഒഴിവാക്കാം എന്നതിനെപ്പറ്റി നമുക്ക് നല്ല ധാരണ ലഭിക്കുന്നതാണ്. ## റിസൾട്ടായി ലഭിക്കുന്നത് ബൂലീൻ ആയിരിക്കും @@ -30,14 +31,52 @@ alert( 2 != 1 ); // true (ശരി) മറ്റേതൊരു വാല്ല്യുവും പോലെ താരതമ്യം ചെയ്ത് കിട്ടുന്ന റിസൾട്ടിനെ ഒരു വേരിയബിളിൽ കൊടുക്കാൻ(assign ചെയ്യാൻ) സാധിക്കും. ```js run -let റിസൾട്ട് = 5 > 4; // റിസൾട്ടിനെ 'റിസൾട്ട്' എന്ന വേരിയബിളിൽ സുക്ഷിക്കുന്നു (assign ചെയ്യുന്നു) -alert(റിസൾട്ട്); // true +let result = 5 > 4; // റിസൾട്ടിനെ 'result' എന്ന വേരിയബിളിൽ സുക്ഷിക്കുന്നു (assign ചെയ്യുന്നു) +alert(result); // true +``` +## സ്ട്രിങ്ങുകളെ താരതമ്യം ചെയ്യൽ + +ഒരു സ്ട്രിംഗ് മറ്റൊന്നിനെക്കാൾ വലുതാണോ എന്നറിയാൻ ജാവാസ്ക്രിപ്റ്റ് "ഡിക്ഷണറി ഓർഡർ" (lexicographical order) ആണ് ഉപയോഗിക്കുന്നത്. + +മറ്റൊരു തരത്തിൽ പറയുകയാണെങ്കിൽ, സ്ട്രിങ്ങുകളെ ഓരോ അക്ഷരം വീതം താരതമ്യം ചെയ്യുന്നു. + +ഉദാഹരണത്തിന്: + +```js run +alert( 'Z' > 'A' ); // true +alert( 'Glow' > 'Glee' ); // true +alert( 'Bee' > 'Be' ); // true + +alert( 'ആ' > 'അ' ); // true +alert( 'കാക്കപ്പെണ്ണ്' > 'കാക്ക' ); // true +``` + +രണ്ട് സ്ട്രിങ്ങുകൾ തമ്മിൽ താരതമ്യം ചെയ്യാൻ ഉപയോഗിക്കുന്ന അൽഗോരിതം വളരെ ലളിതമാണ്: + +1. രണ്ട് സ്ട്രിങ്ങിലെയും ആദ്യത്തെ അക്ഷരങ്ങൾ വീതം താരതമ്യം ചെയ്യുന്നു. +2. ആദ്യത്തെ സ്ട്രിങ്ങിലെ ആദ്യ അക്ഷരം രണ്ടാമത്തെ സ്ട്രിങ്ങിലെ ആദ്യ അക്ഷരത്തെക്കാൾ വലുതാണെങ്കിൽ, ആദ്യത്തെ സ്ട്രിങ്ങാണ് വലുത്. ഇനി തിരിച്ചാണെങ്കിൽ രണ്ടാമത്തെ സ്ട്രിങ്ങാണ് വലുത്. അത്രേയുള്ളു, കഴിഞ്ഞു. +3. ഇനി ആദ്യത്തെ രണ്ട് അക്ഷരങ്ങളും ഒന്നാണെങ്കിൽ, രണ്ട് സ്ട്രിങ്ങിലെയും രണ്ടാമത്തെ അക്ഷരമെടുത്ത് മുമ്പത്തെപ്പോലെ താരതമ്യം ചെയ്യും. +4. സ്ട്രിംഗ് തീരുന്നത് ഇത് തുടരും. +5. രണ്ട് സ്ട്രിങ്ങുകളും ഉൾക്കൊള്ളുന്നത് ഒരേ അക്ഷരങ്ങളാണെങ്കിൽ: സ്ട്രിങ്ങുകളുടെ നീളം തുല്ല്യമാണെങ്കിൽ അവ രണ്ടും തുല്ല്യമാണ്. അല്ലെങ്കിൽ നീളം കൂടിയ സ്ട്രിങ്ങാണ് വലുത്. + +മുകളിലത്തെ ആദ്യത്തെ ഉദാഹരണത്തിൽ (`'Z' > 'A'`), താരതമ്യം ചെയ്യുമ്പോൾ ആദ്യത്തെ സ്റ്റെപ്പിൽ തന്നെ റിസൾട്ട് ലഭിക്കുന്നു. + +എന്നാൽ രണ്ടാമത്തെ ഉദാഹരണത്തിൽ (`'Glow'` and `'Glee'`), റിസൾട്ട് ലഭിക്കാൻ കൂടുതൽ സ്റ്റെപ്പുകൾ ആവശ്യമായി വന്നു: + +1. `G` യും `G` യും തുല്ല്യമാണ്. +2. `l` യും `l` യും തുല്ല്യമാണ്. +3. `o`, `e`യെക്കാൾ വലുതാണ്. താരതമ്യം ചെയ്യൽ ഇവിടെ നിർത്തുന്നു. ആദ്യത്തെ സ്ട്രിങ്ങാണ് വലുത്. + +```smart header="Not a real dictionary, but Unicode order" +മുകളിലത്തെ താരതമ്യ അൽഗൊരിതം ഒരു ഡിക്ഷ്ണറിയിലോ ഫോൺ ബുക്കിലോ വാക്കുകൾ ക്രമീകരിക്കുന്നതിന് ഏകദേശം തുല്ല്യമാണ്. എങ്കിലും അത് അതുപോലെത്തന്നെയല്ല. + +ഉദാഹരണത്തിന്, അക്ഷരം വലുതാണോ ചെറുതാണോ എന്നതിന് ഇതിൽ സ്വാധീനം ഉണ്ട്. അതായത് `"A"` യും `"a"` യും തുല്ല്യമല്ല. ഇതിൽ ഏതാണ് വലുത്? ചെറിയക്ഷരം `"a"`. അത് എന്തുകൊണ്ടോണെന്ന് വെച്ചാൽ ചെറിയക്ഷരങ്ങൾ ആണ് ജാവാസ്ക്രിപ്റ്റ് ഉപയോഗിക്കുന്ന ഇൻ്റേണൽ എൻകോഡിംഗ് ടേബിളിൽ(Unicode) മുകളിൽ വരുന്നത്. ഇതിനെക്കുറിച്ച് വിശദമായി നമ്മൾ ചാപ്റ്ററിൽ പഠിക്കുന്നതാണ്. ``` ## സംഗ്രഹം - താരതമ്യ ഓപറേറ്ററുകൾ ഒരു ബൂലീൻ വാല്യു (`true` or `false`) പുറത്ത് വിടുന്നു (റിട്ടേൺ ചെയ്യുന്നു). - സ്ട്രിങ്ങുകൾ ഓരോ അക്ഷരം വീതമാണ് താരതമ്യം ചെയ്യപ്പെടുന്നത്. ഒരു ഡിക്ഷ്ണറിയിൽ വാക്കുകൾ ക്രമീകരിച്ചിരിക്കുന്നത് പോലെ. -- വ്യത്യസ്ത വിഭാഗങ്ങളിൽപ്പെട്ട വാല്യൂകൾ തമ്മിൽ താരതമ്യം ചെയ്യുമ്പോൾ, അവ ആദ്യം ഒരു നമ്പറിലേക്ക് മാറ്റപ്പെടുന്നു.( സ്ട്രിക്റ്റ് ഇക്വാലിറ്റി ഓപറേറ്ററുകൾ(`===` and `!==`) ഉപയോഗിക്കുമ്പേൾ ഇത് ബാധകമാവുന്നില്ല ). -- `null`,`undefined` വാല്യുകൾ അവയെ പരസ്പരം പൂർണമാക്കുന്നു(`==`). അവ മറ്റൊരു വാല്യുവുമായും തുല്യമാകുന്നില്ല. +- വ്യത്യസ്ത വിഭാഗങ്ങളിൽപ്പെട്ട(different data types) വാല്യൂകൾ തമ്മിൽ താരതമ്യം ചെയ്യുമ്പോൾ, അവ ആദ്യം ഒരു നമ്പറിലേക്ക് മാറ്റപ്പെടുന്നു.( സ്ട്രിക്റ്റ് ഇക്വാലിറ്റി ഓപറേറ്ററുകൾ(`===` and `!==`) ഉപയോഗിക്കുമ്പേൾ ഇത് ബാധകമാവുന്നില്ല ). +- `null`,`undefined` വാല്യുകൾ അവയെ പരസ്പരം പൂർണമാക്കുന്നു(`==`). അവ മറ്റൊരു വാല്യുവുമായും തുല്ല്യമാകുന്നില്ല. - `null/undefined` വാല്ല്യു ലഭിക്കാൻ സാധ്യതയുള്ള വേരിയബ്ൾസുമായി `>` അല്ലെങ്കിൽ `<` പോലെയുള്ള താരതമ്യ ഓപറേറ്ററുകൾ ഉപയോഗിച്ച് താരതമ്യം ചെയ്യുമ്പോൾ ശ്രദ്ധിക്കുക. `null/undefined` വാല്ല്യുകളെ വെവ്വേറെ താരതമ്യം ചെയ്യുന്നതാവും ഉത്തമം. \ No newline at end of file From db0d09d49725e65a767a34348c68837c40802afb Mon Sep 17 00:00:00 2001 From: FarisPalayi Date: Wed, 26 May 2021 20:35:49 +0530 Subject: [PATCH 05/16] Comparisons 'comparison of different types' section Malayalam translation is completed. --- 1-js/02-first-steps/09-comparison/article.md | 45 ++++++++++++++++++-- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/1-js/02-first-steps/09-comparison/article.md b/1-js/02-first-steps/09-comparison/article.md index 5e51481c6..94766b8bf 100644 --- a/1-js/02-first-steps/09-comparison/article.md +++ b/1-js/02-first-steps/09-comparison/article.md @@ -13,9 +13,9 @@ ഇതിൻ്റെ അവസാനം ജാവാസ്ക്രിപ്റ്റിൻ്റെ ഇത്തരം അസ്വാഭാവിക പെരുമാറ്റങ്ങൾ കൊണ്ടുണ്ടാവുന്ന പ്രശ്നങ്ങളെ എങ്ങനെ ഒഴിവാക്കാം എന്നതിനെപ്പറ്റി നമുക്ക് നല്ല ധാരണ ലഭിക്കുന്നതാണ്. -## റിസൾട്ടായി ലഭിക്കുന്നത് ബൂലീൻ ആയിരിക്കും +## റിസൾട്ടായി ലഭിക്കുന്നത് ബൂലിയൻ ആയിരിക്കും -എല്ലാ താരതമ്യ ഓപറേറ്ററുകളും ഒരു ബൂലീൻ വാല്ല്യുവാണ് റിട്ടേൺ ചെയ്യുന്നത്: +എല്ലാ താരതമ്യ ഓപറേറ്ററുകളും ഒരു ബൂലിയൻ വാല്ല്യുവാണ് റിട്ടേൺ ചെയ്യുന്നത്: - `true` -- അർത്ഥം "അതെ", "ശരി" അല്ലെങ്കിൽ "സത്യം". - `false` -- അർത്ഥം "അല്ല", "തെറ്റ്", അല്ലെങ്കിൽ "സത്യമല്ല". @@ -72,10 +72,49 @@ alert( 'കാക്കപ്പെണ്ണ്' > 'കാക്ക' ); // true ഉദാഹരണത്തിന്, അക്ഷരം വലുതാണോ ചെറുതാണോ എന്നതിന് ഇതിൽ സ്വാധീനം ഉണ്ട്. അതായത് `"A"` യും `"a"` യും തുല്ല്യമല്ല. ഇതിൽ ഏതാണ് വലുത്? ചെറിയക്ഷരം `"a"`. അത് എന്തുകൊണ്ടോണെന്ന് വെച്ചാൽ ചെറിയക്ഷരങ്ങൾ ആണ് ജാവാസ്ക്രിപ്റ്റ് ഉപയോഗിക്കുന്ന ഇൻ്റേണൽ എൻകോഡിംഗ് ടേബിളിൽ(Unicode) മുകളിൽ വരുന്നത്. ഇതിനെക്കുറിച്ച് വിശദമായി നമ്മൾ ചാപ്റ്ററിൽ പഠിക്കുന്നതാണ്. ``` +## വ്യത്യസ്ത ടൈപ്പുകളെ(Data types) തമ്മിൽ താരതമ്യം ചെയ്യൽ + +രണ്ട് വ്യത്യസ്ത വിഭാഗത്തിൽ പെട്ട വാല്യുകളെ തമ്മിൽ താരതമ്യം ചെയ്യുമ്പോൾ, ജാവാസ്ക്രിപ്റ്റ് അവയെ ആദ്യം ഒരു നമ്പറിലേക്ക് മാറ്റും. + +ഉദാഹരണത്തിന്: + +```js run +alert( '2' > 1 ); // true, സ്ട്രിംഗ് '2', നമ്പർ 2 ആയി മാറുന്നു. +alert( '01' == 1 ); // true, സ്ട്രിംഗ് '01' നമ്പർ 1 ആയി മാറുന്നു. +``` +ബൂലിയൻ വാല്യുകളിൽ `true` `1` ആയും `false` `0` ആയും മാറുന്നു. + +ഉദാഹരണത്തിന്: + +```js run +alert( true == 1 ); // true +alert( false == 0 ); // true +``` +``` + +````smart header="A funny consequence" +അതേ സമയം ഇതും സാധ്യമാണ്: + +- രണ്ട് വാല്യുകളും തുല്യമാവാം. +- അതിലൊന്ന് `true` ഒരു ബൂലിയനായും, മറ്റൊന്ന് `false` ഒരു ബൂലിയനായും. + + +ഉദാഹരണത്തിന്: + +``` js run +let a = 0; +alert( Boolean(a) ); // false + +let b = "0"; +alert( Boolean(b) ); // true + +alert(a == b); // true! +``` +ജാവാസ്ക്രിപ്റ്റിൻ്റെ ഭാഗത്തു നിന്നു നോക്കുമ്പോൾ ഈ റിസൾട്ട് സാധാരണമാണ്. ഒരു ഇക്വാലിറ്റി ചെക്ക് വാല്യുകളെ നേരിട്ട് നമ്പറിലേക്ക് മാറ്റുമ്പോൾ (അതായത് `"0"`, `0` ആയി മാറുന്നു), `Boolean` മറ്റു പല നിയമങ്ങളും അനുസരിച്ചാണ് ഇതു ചെയ്യുന്നത്. ## സംഗ്രഹം -- താരതമ്യ ഓപറേറ്ററുകൾ ഒരു ബൂലീൻ വാല്യു (`true` or `false`) പുറത്ത് വിടുന്നു (റിട്ടേൺ ചെയ്യുന്നു). +- താരതമ്യ ഓപറേറ്ററുകൾ ഒരു ബൂലിയൻ വാല്യു (`true` or `false`) പുറത്ത് വിടുന്നു (റിട്ടേൺ ചെയ്യുന്നു). - സ്ട്രിങ്ങുകൾ ഓരോ അക്ഷരം വീതമാണ് താരതമ്യം ചെയ്യപ്പെടുന്നത്. ഒരു ഡിക്ഷ്ണറിയിൽ വാക്കുകൾ ക്രമീകരിച്ചിരിക്കുന്നത് പോലെ. - വ്യത്യസ്ത വിഭാഗങ്ങളിൽപ്പെട്ട(different data types) വാല്യൂകൾ തമ്മിൽ താരതമ്യം ചെയ്യുമ്പോൾ, അവ ആദ്യം ഒരു നമ്പറിലേക്ക് മാറ്റപ്പെടുന്നു.( സ്ട്രിക്റ്റ് ഇക്വാലിറ്റി ഓപറേറ്ററുകൾ(`===` and `!==`) ഉപയോഗിക്കുമ്പേൾ ഇത് ബാധകമാവുന്നില്ല ). - `null`,`undefined` വാല്യുകൾ അവയെ പരസ്പരം പൂർണമാക്കുന്നു(`==`). അവ മറ്റൊരു വാല്യുവുമായും തുല്ല്യമാകുന്നില്ല. From 05b9d61be5e5909dd57b644f63e51a20fefb800d Mon Sep 17 00:00:00 2001 From: FarisPalayi Date: Wed, 26 May 2021 21:13:39 +0530 Subject: [PATCH 06/16] =?UTF-8?q?Comparisons=20'strict=20equality=20operat?= =?UTF-8?q?or'=20section=20ml=20translation=20completed=20=E2=9C=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1-js/02-first-steps/09-comparison/article.md | 28 ++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/1-js/02-first-steps/09-comparison/article.md b/1-js/02-first-steps/09-comparison/article.md index 94766b8bf..008e7805d 100644 --- a/1-js/02-first-steps/09-comparison/article.md +++ b/1-js/02-first-steps/09-comparison/article.md @@ -112,6 +112,34 @@ alert(a == b); // true! ``` ജാവാസ്ക്രിപ്റ്റിൻ്റെ ഭാഗത്തു നിന്നു നോക്കുമ്പോൾ ഈ റിസൾട്ട് സാധാരണമാണ്. ഒരു ഇക്വാലിറ്റി ചെക്ക് വാല്യുകളെ നേരിട്ട് നമ്പറിലേക്ക് മാറ്റുമ്പോൾ (അതായത് `"0"`, `0` ആയി മാറുന്നു), `Boolean` മറ്റു പല നിയമങ്ങളും അനുസരിച്ചാണ് ഇതു ചെയ്യുന്നത്. +## സ്ട്രിക്റ്റ് ഇക്വാലിറ്റി ഓപറേറ്റർ + +ഒരു സാധാരണ ഇക്വാലിറ്റി ചെക്കിന്(`==`) ഒരു പ്രശ്നം ഉണ്ട്. അതിന് `0` യെ `false`ൽ നിന്ന് വേർതിരിച്ചറിയാൻ സാധിക്കില്ല. + +```js run +alert( 0 == false ); // true +``` +ഇതേ പ്രശ്നം തന്നെ ഒരു ശൂന്യമായ സ്ട്രിങ്ങിൻ്റെ(empty string) കാര്യത്തിലും ഉണ്ട്. + +```js run +alert( '' == false ); // true +``` + +ഇത് സംഭവിക്കാൻ കാരണം വ്യത്യസ്ത വിഭാഗങ്ങളിൽപ്പെട്ട വാല്യുകളെ ഇക്വാലിറ്റി ഓപറേറ്റർ `==` ഒരു നമ്പറിലേക്ക് മാറ്റുന്നത് കൊണ്ടാണ്. +ഒന്നുമില്ലാത്ത ശൂന്യമായ ഒരു സ്ട്രിംഗ്, `false`നെപ്പോലെത്തന്നെ പൂജ്യമായി മാറുന്നു. + +അപ്പോൾ നമുക്ക് `0` യെ `false`ൽ നിന്നും വേർതിരിച്ചറിയേണ്ടി വന്നാൽ എന്തു ചെയ്യും? + +**ഒരു സ്ട്രിക്റ്റ് ഇക്വാലിറ്റി ഓപറേറ്റർ `===` ടൈപ്പുകളെ മാറ്റാതെ താരതമ്യം ചെയ്യാൻ ഉപയോഗിക്കുന്നു.** +മറ്റൊരു തരത്തിൽ പറയുകയാണെങ്കിൽ, `a`യും `b`യും വ്യത്യസ്ത വിഭാഗങ്ങളിൽപ്പെട്ട വാല്ല്യുകൾ ആണെങ്കിൽ, `a === b` ടൈപ്പുകളെ മാറ്റാതെ തന്നെ ഉടനടി `false` റിട്ടേൺ ചെയ്യുന്നു. + +```js run +alert( 0 === false ); // false, കാരണം വ്യത്യസ്ത ടൈപ്പുകൾ +``` +`==`ന് `!=` ഉള്ളതു പോലെത്തന്നെ `===`ന് ഒരു നോൺ സ്ട്രിക്റ്റ് ഇക്വാലിറ്റി ഓപറേറ്ററും `!==` ഉണ്ട്. + +സ്ട്രിക്റ്റ് ഇക്വാലിറ്റി ഓപറേറ്റർ എഴുതാൻ കുറച്ചധികം ഉണ്ടെങ്കിലും അവ താരതമ്യങ്ങളെ കൂടുതൽ വ്യക്തമാക്കുകയും തെറ്റുകൾ സംഭവിക്കാനുളള സാധ്യത കുറയ്ക്കുകയും ചെയ്യുന്നു. + ## സംഗ്രഹം - താരതമ്യ ഓപറേറ്ററുകൾ ഒരു ബൂലിയൻ വാല്യു (`true` or `false`) പുറത്ത് വിടുന്നു (റിട്ടേൺ ചെയ്യുന്നു). From 3bba2643cabf9252ffc1f3fb5eb76c0713284006 Mon Sep 17 00:00:00 2001 From: FarisPalayi Date: Wed, 26 May 2021 21:44:05 +0530 Subject: [PATCH 07/16] =?UTF-8?q?Comparisons=20'Comparison=20with=20null?= =?UTF-8?q?=20and=20undefined'=20section=20ml=20translation=20completed=20?= =?UTF-8?q?=E2=9C=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1-js/02-first-steps/09-comparison/article.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/1-js/02-first-steps/09-comparison/article.md b/1-js/02-first-steps/09-comparison/article.md index 008e7805d..555ec58b2 100644 --- a/1-js/02-first-steps/09-comparison/article.md +++ b/1-js/02-first-steps/09-comparison/article.md @@ -140,6 +140,25 @@ alert( 0 === false ); // false, കാരണം വ്യത്യസ്ത ട സ്ട്രിക്റ്റ് ഇക്വാലിറ്റി ഓപറേറ്റർ എഴുതാൻ കുറച്ചധികം ഉണ്ടെങ്കിലും അവ താരതമ്യങ്ങളെ കൂടുതൽ വ്യക്തമാക്കുകയും തെറ്റുകൾ സംഭവിക്കാനുളള സാധ്യത കുറയ്ക്കുകയും ചെയ്യുന്നു. +## nullഉം undefinedഉം താരതമ്യം ചെയ്യുമ്പോൾ + +`null` ഉം `undefined` ഉം മറ്റു വാല്യുകളുമായി താരതമ്യം ചെയ്യുമ്പോൾ അപ്രതീക്ഷിതമായ പല സ്വഭാവങ്ങളും നമുക്ക് കാണാൻ സാധിക്കും. + +സ്ട്രിക്റ്റ് ഇക്വാലിറ്റി ഓപറേറ്റർ `===` ഉപയോഗിക്കുമ്പോൾ +: വാല്യുകൾ വ്യത്യസ്തമാണ്, കാരണം ഇവ രണ്ടും വ്യത്യസ്ത വിഭാഗമാണ്(data type). + + ```js run + alert( null === undefined ); // false + ``` +നോൺ സ്ട്രിക്റ്റ് ഓപറേറ്റർ `==` ഉപയോഗിക്കുമ്പോൾ +: ഇവിടെ ഒരു പ്രത്യേക നിയമമുണ്ട്: `null`,`undefined` വാല്യുകൾ അവയെ പരസ്പരം പൂർണമാക്കുന്നു. അവ മറ്റൊരു വാല്യുവുമായും തുല്ല്യമാകുന്നില്ല. + + ```js run + alert( null == undefined ); // true + ``` +മറ്റു ഓപറേറ്ററുകൾ ഉപയോഗിക്കുമ്പോൾ `< > <= >=` +: `null` ഉം `undefined` ഉം നമ്പറിലേക്ക് മാറ്റപ്പെടുന്നു: `null` `0` ആയും, `undefined` `NaN` ആയും മാറുന്നു. +ഇനി നമുക്ക് ഈ നിയമങ്ങൾ ഉപയോഗിച്ചാൽ സംഭവിക്കുന്ന ചില രസകരമായ കാര്യങ്ങൾ കാണാം. അതിലും പ്രധാനമായി എങ്ങനെ ഇത്തരം കെണികളിൽ വീഴാതെ നോക്കാം എന്നും. ## സംഗ്രഹം - താരതമ്യ ഓപറേറ്ററുകൾ ഒരു ബൂലിയൻ വാല്യു (`true` or `false`) പുറത്ത് വിടുന്നു (റിട്ടേൺ ചെയ്യുന്നു). From c1c1ce9d50bcb2c8c936f44e29efe79e74fd1817 Mon Sep 17 00:00:00 2001 From: FarisPalayi Date: Wed, 26 May 2021 22:22:05 +0530 Subject: [PATCH 08/16] =?UTF-8?q?Comparisons=20'Strange=20result:=20null?= =?UTF-8?q?=20vs=200'=20section=20ml=20translation=20completed=20=E2=9C=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1-js/02-first-steps/09-comparison/article.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/1-js/02-first-steps/09-comparison/article.md b/1-js/02-first-steps/09-comparison/article.md index 555ec58b2..1f0808b5b 100644 --- a/1-js/02-first-steps/09-comparison/article.md +++ b/1-js/02-first-steps/09-comparison/article.md @@ -131,6 +131,7 @@ alert( '' == false ); // true അപ്പോൾ നമുക്ക് `0` യെ `false`ൽ നിന്നും വേർതിരിച്ചറിയേണ്ടി വന്നാൽ എന്തു ചെയ്യും? **ഒരു സ്ട്രിക്റ്റ് ഇക്വാലിറ്റി ഓപറേറ്റർ `===` ടൈപ്പുകളെ മാറ്റാതെ താരതമ്യം ചെയ്യാൻ ഉപയോഗിക്കുന്നു.** + മറ്റൊരു തരത്തിൽ പറയുകയാണെങ്കിൽ, `a`യും `b`യും വ്യത്യസ്ത വിഭാഗങ്ങളിൽപ്പെട്ട വാല്ല്യുകൾ ആണെങ്കിൽ, `a === b` ടൈപ്പുകളെ മാറ്റാതെ തന്നെ ഉടനടി `false` റിട്ടേൺ ചെയ്യുന്നു. ```js run @@ -159,6 +160,23 @@ alert( 0 === false ); // false, കാരണം വ്യത്യസ്ത ട മറ്റു ഓപറേറ്ററുകൾ ഉപയോഗിക്കുമ്പോൾ `< > <= >=` : `null` ഉം `undefined` ഉം നമ്പറിലേക്ക് മാറ്റപ്പെടുന്നു: `null` `0` ആയും, `undefined` `NaN` ആയും മാറുന്നു. ഇനി നമുക്ക് ഈ നിയമങ്ങൾ ഉപയോഗിച്ചാൽ സംഭവിക്കുന്ന ചില രസകരമായ കാര്യങ്ങൾ കാണാം. അതിലും പ്രധാനമായി എങ്ങനെ ഇത്തരം കെണികളിൽ വീഴാതെ നോക്കാം എന്നും. + +### വിചിത്രമായ റിസൾട്ട്: null vs 0 + +`null`നെ പൂജ്യവുമായി താരതമ്യം ചെയ്യാം + +```js run +alert( null > 0 ); // (1) false +alert( null == 0 ); // (2) false +alert( null >= 0 ); // (3) *!*true*/!* +``` +ഗണിതപരമായി നോക്കുമ്പോൾ ഇത് വിചിത്രമാണ്. കാരണം, അവസാന റിസൾട്ട് പറയുന്നത് "`null` പൂജ്യത്തിനെക്കാൾ വലുതോ തുല്ല്യമോ ആണ് എന്നാണ്", +അപ്പോൾ മുകളിലുളളതിൽ ഏതെങ്കിലുമൊന്ന് `true` ആവേണ്ടതാണ്. പക്ഷെ അവ രണ്ടും `false` ആണ്. + +ഇതിനു കാരണം ഒരു ഇക്വാലിറ്റി ചെക്കും `==` മറ്റു താരതമ്യ ഓപറേറ്ററുകളും `> < >= <=` വ്യത്യസ്തമായാണ് പ്രവർത്തിക്കുന്നത്. `> < >= <=` `null`നെ ഒരു നമ്പറിലേക്ക് മാറ്റുന്നു, അതായത് പൂജ്യത്തിലേക്ക്. അതുകൊണ്ടാണ് (3) `null >= 0` trueയും (1) `null > 0` falseഉം റിട്ടേൺ ചെയ്യുന്നത്. + +ഇക്വാലിറ്റി ചെക്ക് `==` ഉപയോഗിച്ച് `undefined`നെയും `null`നെയും താരതമ്യം ചെയ്യുമ്പോൾ, അവ പരസ്പരമല്ലാതെ മറ്റൊന്നുമായും തുല്ല്യമാവുകയോ മറ്റൊരു typeലേക്ക് മാറ്റപ്പെടുകയോ ഇല്ല. അത് കൊണ്ടാണ് (2) `null == 0` false റിട്ടേൺ ചെയ്യുന്നത്. + ## സംഗ്രഹം - താരതമ്യ ഓപറേറ്ററുകൾ ഒരു ബൂലിയൻ വാല്യു (`true` or `false`) പുറത്ത് വിടുന്നു (റിട്ടേൺ ചെയ്യുന്നു). From a60288e6623bf2f1cd0262f0506eca0eea11e611 Mon Sep 17 00:00:00 2001 From: FarisPalayi Date: Wed, 26 May 2021 22:39:48 +0530 Subject: [PATCH 09/16] =?UTF-8?q?Comparisons=20'An=20incomparable=20undefi?= =?UTF-8?q?ned'=20section=20ml=20translation=20completed=20=E2=9C=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1-js/02-first-steps/09-comparison/article.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/1-js/02-first-steps/09-comparison/article.md b/1-js/02-first-steps/09-comparison/article.md index 1f0808b5b..c4f701eb0 100644 --- a/1-js/02-first-steps/09-comparison/article.md +++ b/1-js/02-first-steps/09-comparison/article.md @@ -177,6 +177,22 @@ alert( null >= 0 ); // (3) *!*true*/!* ഇക്വാലിറ്റി ചെക്ക് `==` ഉപയോഗിച്ച് `undefined`നെയും `null`നെയും താരതമ്യം ചെയ്യുമ്പോൾ, അവ പരസ്പരമല്ലാതെ മറ്റൊന്നുമായും തുല്ല്യമാവുകയോ മറ്റൊരു typeലേക്ക് മാറ്റപ്പെടുകയോ ഇല്ല. അത് കൊണ്ടാണ് (2) `null == 0` false റിട്ടേൺ ചെയ്യുന്നത്. +### താരതമ്യം ചെയ്യാനാവാത്ത undefined + +`undefined`നെ ഒരിക്കലും മറ്റൊരു വാല്ല്യുവുമായും താരതമ്യം ചെയ്യരുത്: + +```js run +alert( undefined > 0 ); // false (1) +alert( undefined < 0 ); // false (2) +alert( undefined == 0 ); // false (3) +``` + +എന്തുകൊണ്ടാണ് undefinedന് പൂജ്യത്തിനോട് ഇത്രക്കും വിരോധം? എപ്പോഴും false. + +ഇതിന് കാരണം: +- `(1)`ാമത്തെയും `(2)`ാമത്തെയും താരതമ്യങ്ങൾ `false` റിട്ടേൺ ചെയ്യുന്നത് `undefined` `NaN`ലേക്ക് മാറ്റപ്പെടുന്നത് കൊണ്ടാണ്. `NaN`നെ ഏതൊരു വാല്യുവുമായും താരതമ്യം ചെയ്താലും `false` ആണ് ലഭിക്കുക. +- `(3)`ാമത്തെത് `false` ആവാൻ കാരണം `undefined` `null` ഒഴികെ മറ്റൊരു വാല്യുവുമായും തുല്ല്യമാവാത്തത് കൊണ്ടാണ്. + ## സംഗ്രഹം - താരതമ്യ ഓപറേറ്ററുകൾ ഒരു ബൂലിയൻ വാല്യു (`true` or `false`) പുറത്ത് വിടുന്നു (റിട്ടേൺ ചെയ്യുന്നു). From 3af48f4c5c4404bb0294b23d58dc0372075f3f17 Mon Sep 17 00:00:00 2001 From: FarisPalayi Date: Wed, 26 May 2021 23:38:04 +0530 Subject: [PATCH 10/16] =?UTF-8?q?Comparisons=20'Avoid=20problems'=20sectio?= =?UTF-8?q?n=20Malayalam=20translation=20completed=20=E2=9C=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1-js/02-first-steps/09-comparison/article.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/1-js/02-first-steps/09-comparison/article.md b/1-js/02-first-steps/09-comparison/article.md index c4f701eb0..9a37582fd 100644 --- a/1-js/02-first-steps/09-comparison/article.md +++ b/1-js/02-first-steps/09-comparison/article.md @@ -193,9 +193,15 @@ alert( undefined == 0 ); // false (3) - `(1)`ാമത്തെയും `(2)`ാമത്തെയും താരതമ്യങ്ങൾ `false` റിട്ടേൺ ചെയ്യുന്നത് `undefined` `NaN`ലേക്ക് മാറ്റപ്പെടുന്നത് കൊണ്ടാണ്. `NaN`നെ ഏതൊരു വാല്യുവുമായും താരതമ്യം ചെയ്താലും `false` ആണ് ലഭിക്കുക. - `(3)`ാമത്തെത് `false` ആവാൻ കാരണം `undefined` `null` ഒഴികെ മറ്റൊരു വാല്യുവുമായും തുല്ല്യമാവാത്തത് കൊണ്ടാണ്. +### തെറ്റുകൾ ഒഴിവാക്കാം + +നമ്മളെന്തിനാണ് ഈ ഉദാഹരണങ്ങളിലൂടെ മുഴുവൻ കടന്നു പോയത്? ഇത്തരം എല്ലാ ചെറിയ 'അസ്വാഭാവിക' കാര്യങ്ങളും നമ്മൾ ഓർത്തു വെക്കേണ്ടതുണ്ടോ? ശരിക്കും പറഞ്ഞാൽ വേണ്ട. കാരണം ഇത്തരം tricky കാര്യങ്ങൾ പതിയെപ്പതിയെ നമുക്ക് പരിചിതമാവും. എങ്കിലും ഇത്തരം പ്രശ്നങ്ങൾ ഒഴിവാക്കാൻ ഒരു നല്ല മാർഗമുണ്ട്: + +- `undefined/null` താരതമ്യം ചെയ്യാൻ സ്ട്രിക്റ്റ് ഇക്വാലിറ്റി ഓപറേറ്ററുകൾ `===` അല്ലാത്ത ഓപറേറ്ററുകൾ ഉപയോഗിക്കുമ്പോൾ വളരെയധികം ശ്രദ്ധിക്കുക. +- `null/undefined` വാല്ല്യു ലഭിക്കാൻ സാധ്യതയുള്ള വേരിയബ്ൾസുമായി `>= > < <=` ഓപറേറ്ററുകൾ ഉപയോഗിച്ച് ഒരിക്കലും താരതമ്യം ചെയ്യരുത്, ചെയ്യുന്നതെന്താണെന്ന് അത്രയ്ക്കും ഉറപ്പുണ്ടെങ്കിലൊഴിച്ച്. വേരിയബ്ൾസിന് ഈ വാല്ല്യുകൾ ഉണ്ടാവാൻ സാധ്യതയുണ്ടെങ്കിൽ അത് ആദ്യം വെവ്വേറെയായി ചെക്ക് ചെയ്യുക. ## സംഗ്രഹം -- താരതമ്യ ഓപറേറ്ററുകൾ ഒരു ബൂലിയൻ വാല്യു (`true` or `false`) പുറത്ത് വിടുന്നു (റിട്ടേൺ ചെയ്യുന്നു). +- താരതമ്യ ഓപറേറ്ററുകൾ ഒരു ബൂലിയൻ വാല്യു (`true` or `false`) റിട്ടേൺ ചെയ്യുന്നു. - സ്ട്രിങ്ങുകൾ ഓരോ അക്ഷരം വീതമാണ് താരതമ്യം ചെയ്യപ്പെടുന്നത്. ഒരു ഡിക്ഷ്ണറിയിൽ വാക്കുകൾ ക്രമീകരിച്ചിരിക്കുന്നത് പോലെ. - വ്യത്യസ്ത വിഭാഗങ്ങളിൽപ്പെട്ട(different data types) വാല്യൂകൾ തമ്മിൽ താരതമ്യം ചെയ്യുമ്പോൾ, അവ ആദ്യം ഒരു നമ്പറിലേക്ക് മാറ്റപ്പെടുന്നു.( സ്ട്രിക്റ്റ് ഇക്വാലിറ്റി ഓപറേറ്ററുകൾ(`===` and `!==`) ഉപയോഗിക്കുമ്പേൾ ഇത് ബാധകമാവുന്നില്ല ). - `null`,`undefined` വാല്യുകൾ അവയെ പരസ്പരം പൂർണമാക്കുന്നു(`==`). അവ മറ്റൊരു വാല്യുവുമായും തുല്ല്യമാകുന്നില്ല. From 61df95f696dfd01ad58bf45da946d1d4fbdf1e13 Mon Sep 17 00:00:00 2001 From: FarisPalayi Date: Thu, 27 May 2021 00:50:42 +0530 Subject: [PATCH 11/16] Comparisons adding 'smart headers' ml translation --- .../09-comparison/article-en.md | 216 ------------------ 1-js/02-first-steps/09-comparison/article.md | 18 +- 2 files changed, 10 insertions(+), 224 deletions(-) delete mode 100644 1-js/02-first-steps/09-comparison/article-en.md diff --git a/1-js/02-first-steps/09-comparison/article-en.md b/1-js/02-first-steps/09-comparison/article-en.md deleted file mode 100644 index ead7922ff..000000000 --- a/1-js/02-first-steps/09-comparison/article-en.md +++ /dev/null @@ -1,216 +0,0 @@ -# Comparisons - -We know many comparison operators from maths. - -In JavaScript they are written like this: - -- Greater/less than: a > b, a < b. -- Greater/less than or equals: a >= b, a <= b. -- Equals: `a == b`, please note the double equality sign `==` means the equality test, while a single one `a = b` means an assignment. -- Not equals. In maths the notation is , but in JavaScript it's written as a != b. - -In this article we'll learn more about different types of comparisons, how JavaScript makes them, including important peculiarities. - -At the end you'll find a good recipe to avoid "JavaScript quirks"-related issues. - -## Boolean is the result - -All comparison operators return a boolean value: - -- `true` -- means "yes", "correct" or "the truth". -- `false` -- means "no", "wrong" or "not the truth". - -For example: - -```js run -alert( 2 > 1 ); // true (correct) -alert( 2 == 1 ); // false (wrong) -alert( 2 != 1 ); // true (correct) -``` - -A comparison result can be assigned to a variable, just like any value: - -```js run -let result = 5 > 4; // assign the result of the comparison -alert( result ); // true -``` - -## String comparison - -To see whether a string is greater than another, JavaScript uses the so-called "dictionary" or "lexicographical" order. - -In other words, strings are compared letter-by-letter. - -For example: - -```js run -alert( 'Z' > 'A' ); // true -alert( 'Glow' > 'Glee' ); // true -alert( 'Bee' > 'Be' ); // true -``` - -The algorithm to compare two strings is simple: - -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. - -In the first example above, the comparison `'Z' > 'A'` gets to a result at the first step. - -The second comparison `'Glow'` and `'Glee'` needs more steps as strings are compared character-by-character: - -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. - -```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. - -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 . -``` - -## Comparison of different types - -When comparing values of different types, JavaScript converts the values to numbers. - -For example: - -```js run -alert( '2' > 1 ); // true, string '2' becomes a number 2 -alert( '01' == 1 ); // true, string '01' becomes a number 1 -``` - -For boolean values, `true` becomes `1` and `false` becomes `0`. - -For example: - -```js run -alert( true == 1 ); // true -alert( false == 0 ); // true -``` - -````smart header="A funny consequence" -It is possible that at the same time: - -- Two values are equal. -- One of them is `true` as a boolean and the other one is `false` as a boolean. - -For example: - -```js run -let a = 0; -alert( Boolean(a) ); // false - -let b = "0"; -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. -```` - -## Strict equality - -A regular equality check `==` has a problem. It cannot differentiate `0` from `false`: - -```js run -alert( 0 == false ); // true -``` - -The same thing happens with an empty string: - -```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. - -What to do if we'd like to differentiate `0` from `false`? - -**A strict equality operator `===` checks the equality without type conversion.** - -In other words, if `a` and `b` are of different types, then `a === b` immediately returns `false` without an attempt to convert them. - -Let's try it: - -```js run -alert( 0 === false ); // false, because the types are different -``` - -There is also a "strict non-equality" operator `!==` analogous to `!=`. - -The strict equality operator is a bit longer to write, but makes it obvious what's going on and leaves less room for errors. - -## Comparison with null and undefined - -There's a non-intuitive behavior when `null` or `undefined` are compared to other values. - -For a strict equality check `===` -: These values are different, because each of them is a different type. - - ```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. - - ```js run - alert( null == undefined ); // true - ``` - -For maths and other comparisons `< > <= >=` -: `null/undefined` are converted to numbers: `null` becomes `0`, while `undefined` becomes `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. - -### Strange result: null vs 0 - -Let's compare `null` with a zero: - -```js run -alert( null > 0 ); // (1) false -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. - -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. - -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. - -### An incomparable undefined - -The value `undefined` shouldn't be compared to other values: - -```js run -alert( undefined > 0 ); // false (1) -alert( undefined < 0 ); // false (2) -alert( undefined == 0 ); // false (3) -``` - -Why does it dislike zero so much? Always false! - -We get these results because: - -- 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. - -### Avoid problems - -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 avoid problems with them: - -- Treat any comparison with `undefined/null` except the strict equality `===` with exceptional care. -- 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. - -## Summary - -- 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. diff --git a/1-js/02-first-steps/09-comparison/article.md b/1-js/02-first-steps/09-comparison/article.md index 9a37582fd..fe10892cd 100644 --- a/1-js/02-first-steps/09-comparison/article.md +++ b/1-js/02-first-steps/09-comparison/article.md @@ -1,4 +1,4 @@ -# താരതമ്യപ്പെടുത്തൽ +# താരതമ്യം ചെയ്യൽ ഗണിതത്തിൽ സംഖ്യകളെ പരസ്പരം താരതമ്യം ചെയ്യുന്നത് എങ്ങനെയെന്ന് നമുക്കറിയാം. @@ -34,7 +34,7 @@ alert( 2 != 1 ); // true (ശരി) let result = 5 > 4; // റിസൾട്ടിനെ 'result' എന്ന വേരിയബിളിൽ സുക്ഷിക്കുന്നു (assign ചെയ്യുന്നു) alert(result); // true ``` -## സ്ട്രിങ്ങുകളെ താരതമ്യം ചെയ്യൽ +## സ്ട്രിങ്ങുകളെ താരതമ്യം ചെയ്യുമ്പോൾ ഒരു സ്ട്രിംഗ് മറ്റൊന്നിനെക്കാൾ വലുതാണോ എന്നറിയാൻ ജാവാസ്ക്രിപ്റ്റ് "ഡിക്ഷണറി ഓർഡർ" (lexicographical order) ആണ് ഉപയോഗിക്കുന്നത്. @@ -67,12 +67,12 @@ alert( 'കാക്കപ്പെണ്ണ്' > 'കാക്ക' ); // true 2. `l` യും `l` യും തുല്ല്യമാണ്. 3. `o`, `e`യെക്കാൾ വലുതാണ്. താരതമ്യം ചെയ്യൽ ഇവിടെ നിർത്തുന്നു. ആദ്യത്തെ സ്ട്രിങ്ങാണ് വലുത്. -```smart header="Not a real dictionary, but Unicode order" +```smart header="ഒരു യഥാർത്ഥ ഡിക്ഷ്ണറി അല്ല, പക്ഷെ യൂനികോഡ് ഓർഡർ" മുകളിലത്തെ താരതമ്യ അൽഗൊരിതം ഒരു ഡിക്ഷ്ണറിയിലോ ഫോൺ ബുക്കിലോ വാക്കുകൾ ക്രമീകരിക്കുന്നതിന് ഏകദേശം തുല്ല്യമാണ്. എങ്കിലും അത് അതുപോലെത്തന്നെയല്ല. ഉദാഹരണത്തിന്, അക്ഷരം വലുതാണോ ചെറുതാണോ എന്നതിന് ഇതിൽ സ്വാധീനം ഉണ്ട്. അതായത് `"A"` യും `"a"` യും തുല്ല്യമല്ല. ഇതിൽ ഏതാണ് വലുത്? ചെറിയക്ഷരം `"a"`. അത് എന്തുകൊണ്ടോണെന്ന് വെച്ചാൽ ചെറിയക്ഷരങ്ങൾ ആണ് ജാവാസ്ക്രിപ്റ്റ് ഉപയോഗിക്കുന്ന ഇൻ്റേണൽ എൻകോഡിംഗ് ടേബിളിൽ(Unicode) മുകളിൽ വരുന്നത്. ഇതിനെക്കുറിച്ച് വിശദമായി നമ്മൾ ചാപ്റ്ററിൽ പഠിക്കുന്നതാണ്. ``` -## വ്യത്യസ്ത ടൈപ്പുകളെ(Data types) തമ്മിൽ താരതമ്യം ചെയ്യൽ +## വ്യത്യസ്ത ടൈപ്പുകളെ(Data types) തമ്മിൽ താരതമ്യം ചെയ്യുമ്പോൾ രണ്ട് വ്യത്യസ്ത വിഭാഗത്തിൽ പെട്ട വാല്യുകളെ തമ്മിൽ താരതമ്യം ചെയ്യുമ്പോൾ, ജാവാസ്ക്രിപ്റ്റ് അവയെ ആദ്യം ഒരു നമ്പറിലേക്ക് മാറ്റും. @@ -92,7 +92,7 @@ alert( false == 0 ); // true ``` ``` -````smart header="A funny consequence" +````smart header="രസകരമായ അനന്തരഫലം" അതേ സമയം ഇതും സാധ്യമാണ്: - രണ്ട് വാല്യുകളും തുല്യമാവാം. @@ -101,7 +101,7 @@ alert( false == 0 ); // true ഉദാഹരണത്തിന്: -``` js run +```js run let a = 0; alert( Boolean(a) ); // false @@ -110,6 +110,8 @@ alert( Boolean(b) ); // true alert(a == b); // true! ``` +```` + ജാവാസ്ക്രിപ്റ്റിൻ്റെ ഭാഗത്തു നിന്നു നോക്കുമ്പോൾ ഈ റിസൾട്ട് സാധാരണമാണ്. ഒരു ഇക്വാലിറ്റി ചെക്ക് വാല്യുകളെ നേരിട്ട് നമ്പറിലേക്ക് മാറ്റുമ്പോൾ (അതായത് `"0"`, `0` ആയി മാറുന്നു), `Boolean` മറ്റു പല നിയമങ്ങളും അനുസരിച്ചാണ് ഇതു ചെയ്യുന്നത്. ## സ്ട്രിക്റ്റ് ഇക്വാലിറ്റി ഓപറേറ്റർ @@ -141,7 +143,7 @@ alert( 0 === false ); // false, കാരണം വ്യത്യസ്ത ട സ്ട്രിക്റ്റ് ഇക്വാലിറ്റി ഓപറേറ്റർ എഴുതാൻ കുറച്ചധികം ഉണ്ടെങ്കിലും അവ താരതമ്യങ്ങളെ കൂടുതൽ വ്യക്തമാക്കുകയും തെറ്റുകൾ സംഭവിക്കാനുളള സാധ്യത കുറയ്ക്കുകയും ചെയ്യുന്നു. -## nullഉം undefinedഉം താരതമ്യം ചെയ്യുമ്പോൾ +## null, undefined - താരതമ്യം ചെയ്യുമ്പോൾ `null` ഉം `undefined` ഉം മറ്റു വാല്യുകളുമായി താരതമ്യം ചെയ്യുമ്പോൾ അപ്രതീക്ഷിതമായ പല സ്വഭാവങ്ങളും നമുക്ക് കാണാൻ സാധിക്കും. @@ -161,7 +163,7 @@ alert( 0 === false ); // false, കാരണം വ്യത്യസ്ത ട : `null` ഉം `undefined` ഉം നമ്പറിലേക്ക് മാറ്റപ്പെടുന്നു: `null` `0` ആയും, `undefined` `NaN` ആയും മാറുന്നു. ഇനി നമുക്ക് ഈ നിയമങ്ങൾ ഉപയോഗിച്ചാൽ സംഭവിക്കുന്ന ചില രസകരമായ കാര്യങ്ങൾ കാണാം. അതിലും പ്രധാനമായി എങ്ങനെ ഇത്തരം കെണികളിൽ വീഴാതെ നോക്കാം എന്നും. -### വിചിത്രമായ റിസൾട്ട്: null vs 0 +### അസാധാരണ റിസൾട്ടുകൾ: null vs 0 `null`നെ പൂജ്യവുമായി താരതമ്യം ചെയ്യാം From 30febc873d097d983aa3a5eac9bfec6f98014a84 Mon Sep 17 00:00:00 2001 From: FarisPalayi Date: Thu, 27 May 2021 01:24:26 +0530 Subject: [PATCH 12/16] Removing some ml charcters, 'cause it makes things bloated. --- 1-js/02-first-steps/09-comparison/article.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/1-js/02-first-steps/09-comparison/article.md b/1-js/02-first-steps/09-comparison/article.md index fe10892cd..6f2cecbb8 100644 --- a/1-js/02-first-steps/09-comparison/article.md +++ b/1-js/02-first-steps/09-comparison/article.md @@ -4,10 +4,10 @@ ജാവാസ്ക്രിപ്റ്റിൽ അത് ഇങ്ങനെയാണ് : -- ഒന്ന് മറ്റൊന്നിനെക്കാൾ വലുത്/ചെറുത് : അ > ആ, അ < ആ (a > b, a < b). -- ഒന്ന് മറ്റൊന്നിനെക്കാൾ വലുത്/ചെറുത് അല്ലെങ്കിൽ തുല്ല്യം : അ >= ആ, അ <= ആ (a >= b, a <= b). -- തുല്ല്യം/സമം : `അ === ആ`. ശ്രദ്ധിക്കുക രണ്ട് സമചിഹ്നം (`അ == ആ`) തുല്ല്യമാണോ എന്ന് പരിശോധിക്കാൻ ഉപയോഗിക്കുന്നു. എന്നാൽ ഒരു സമചിഹ്നം (`അ = ആ`) കൊണ്ടുദ്ദേശിക്കുന്നത് 'വാല്ല്യു കൊടുക്കുക (assignment)' എന്നാണ്. -- തുല്ല്യമല്ല : ഗണിതത്തിൽ ഇതിനുപയോഗിക്കുന്നത് ചിഹ്നമാണ്, എന്നാൽ ജാവാസ്ക്രിപ്റ്റിൽ അതിങ്ങനെയാണ് എഴുതുന്നത്: അ != ആ (a != b). +- ഒന്ന് മറ്റൊന്നിനെക്കാൾ വലുത്/ചെറുത് : a > b, a < b. +- ഒന്ന് മറ്റൊന്നിനെക്കാൾ വലുത്/ചെറുത് അല്ലെങ്കിൽ തുല്ല്യം : a >= b, a <= b. +- തുല്ല്യം/സമം : `a === b`. ശ്രദ്ധിക്കുക രണ്ട് സമചിഹ്നം (`a == b`) തുല്ല്യമാണോ എന്ന് പരിശോധിക്കാൻ ഉപയോഗിക്കുന്നു. എന്നാൽ ഒരു സമചിഹ്നം (`a = b`) കൊണ്ടുദ്ദേശിക്കുന്നത് 'വാല്ല്യു കൊടുക്കുക (assignment)' എന്നാണ്. +- തുല്ല്യമല്ല : ഗണിതത്തിൽ ഇതിനുപയോഗിക്കുന്നത് ചിഹ്നമാണ്, എന്നാൽ ജാവാസ്ക്രിപ്റ്റിൽ അതിങ്ങനെയാണ് എഴുതുന്നത്: a != b. ഈ ലേഖനത്തിൽ പലതരം താരതമ്യപ്പെടുത്തലുകളെക്കുറിച്ചും, ജാവാസ്ക്രിപ്റ്റ് അവയെ എങ്ങനെയാണ് നിർമിക്കുന്നതെന്നും, നമ്മൾ പഠിക്കും. ജാവാസ്ക്രിപ്റ്റിൻ്റെ ചില പ്രധാനപ്പെട്ട 'അസ്വാഭാവിക പെരുമാറ്റങ്ങൾ' ഉൾപ്പെടെ. @@ -82,7 +82,7 @@ alert( 'കാക്കപ്പെണ്ണ്' > 'കാക്ക' ); // true alert( '2' > 1 ); // true, സ്ട്രിംഗ് '2', നമ്പർ 2 ആയി മാറുന്നു. alert( '01' == 1 ); // true, സ്ട്രിംഗ് '01' നമ്പർ 1 ആയി മാറുന്നു. ``` -ബൂലിയൻ വാല്യുകളിൽ `true` `1` ആയും `false` `0` ആയും മാറുന്നു. +ബൂലിയൻ വാല്യുകളിൽ `true`, `1` ആയും `false`, `0` ആയും മാറുന്നു. ഉദാഹരണത്തിന്: From 2a9d9501274f43f9d8be2163e517a909b86604f8 Mon Sep 17 00:00:00 2001 From: FarisPalayi Date: Thu, 27 May 2021 01:29:57 +0530 Subject: [PATCH 13/16] Adding task and solution ml translation -> Comparisons article --- .../1-comparison-questions/solution.md | 16 ++++++++-------- .../09-comparison/1-comparison-questions/task.md | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/1-js/02-first-steps/09-comparison/1-comparison-questions/solution.md b/1-js/02-first-steps/09-comparison/1-comparison-questions/solution.md index 632b1cf4e..1e5ddda70 100644 --- a/1-js/02-first-steps/09-comparison/1-comparison-questions/solution.md +++ b/1-js/02-first-steps/09-comparison/1-comparison-questions/solution.md @@ -10,12 +10,12 @@ null == "\n0\n" → false null === +"\n0\n" → false ``` -Some of the reasons: +കാരണങ്ങൾ: -1. Obviously, true. -2. Dictionary comparison, hence false. `"a"` is smaller than `"p"`. -3. Again, dictionary comparison, first char `"2"` is greater than the first char `"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. സംശയമില്ല, true. +2. ഡിക്ഷ്ണറി ഓർഡറിൽ താരതമ്യം ചെയ്യുന്നു, അതിനാൽ false. `"a"`, `"p"` യെക്കാൾ ചെറുതാണ്. +3. ഇതും ഡിക്ഷ്ണറി ഓർഡറിൽ താരതമ്യം ചെയ്യുന്നു, ആദ്യ അക്ഷരം `"2"`, ആദ്യ അക്ഷരം `"1"`നെക്കാൾ വലുതാണ്. +4. `null`,`undefined` വാല്യുകൾ അവയെ പരസ്പരം മാത്രം പൂർണമാക്കുന്നു(`==`). +5. സ്ട്രിക്റ്റ് ഇക്വാലിറ്റി ഓപറേറ്റർ സ്ട്രിക്റ്റ് ആണ്. വ്യത്യസ്ത ഡാറ്റാടൈപ്പുകൾ false റിട്ടേൺ ചെയ്യുന്നു. +6. `(4)`പോലെത്തന്നെ, `null`,`undefined` വാല്യുകൾ അവയെ പരസ്പരം മാത്രമേ പൂർണമാക്കുകയുളളൂ. +7. സ്ട്രിക്റ്റ് ഇക്വാലിറ്റി, വ്യത്യസ്ത ഡാറ്റാടൈപ്പുകൾ -> false \ No newline at end of file diff --git a/1-js/02-first-steps/09-comparison/1-comparison-questions/task.md b/1-js/02-first-steps/09-comparison/1-comparison-questions/task.md index be7f75ddd..a31c5160c 100644 --- a/1-js/02-first-steps/09-comparison/1-comparison-questions/task.md +++ b/1-js/02-first-steps/09-comparison/1-comparison-questions/task.md @@ -4,7 +4,7 @@ importance: 5 # Comparisons -What will be the result for these expressions? +ഇവയുടെ റിസൾട്ട് എന്തായിരിക്കും? ```js no-beautify 5 > 4 From 9abfd849eae2f0cbcd5e5959660e6e4935c1bf62 Mon Sep 17 00:00:00 2001 From: Sid <35549603+siddiqkaithodu@users.noreply.github.com> Date: Thu, 27 May 2021 10:49:55 +0530 Subject: [PATCH 14/16] Update solution.md --- .../09-comparison/1-comparison-questions/solution.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/1-js/02-first-steps/09-comparison/1-comparison-questions/solution.md b/1-js/02-first-steps/09-comparison/1-comparison-questions/solution.md index 1e5ddda70..b29adaa51 100644 --- a/1-js/02-first-steps/09-comparison/1-comparison-questions/solution.md +++ b/1-js/02-first-steps/09-comparison/1-comparison-questions/solution.md @@ -12,10 +12,10 @@ null === +"\n0\n" → false കാരണങ്ങൾ: -1. സംശയമില്ല, true. +1. സംശയമില്ല, സത്യം. 2. ഡിക്ഷ്ണറി ഓർഡറിൽ താരതമ്യം ചെയ്യുന്നു, അതിനാൽ false. `"a"`, `"p"` യെക്കാൾ ചെറുതാണ്. 3. ഇതും ഡിക്ഷ്ണറി ഓർഡറിൽ താരതമ്യം ചെയ്യുന്നു, ആദ്യ അക്ഷരം `"2"`, ആദ്യ അക്ഷരം `"1"`നെക്കാൾ വലുതാണ്. 4. `null`,`undefined` വാല്യുകൾ അവയെ പരസ്പരം മാത്രം പൂർണമാക്കുന്നു(`==`). 5. സ്ട്രിക്റ്റ് ഇക്വാലിറ്റി ഓപറേറ്റർ സ്ട്രിക്റ്റ് ആണ്. വ്യത്യസ്ത ഡാറ്റാടൈപ്പുകൾ false റിട്ടേൺ ചെയ്യുന്നു. 6. `(4)`പോലെത്തന്നെ, `null`,`undefined` വാല്യുകൾ അവയെ പരസ്പരം മാത്രമേ പൂർണമാക്കുകയുളളൂ. -7. സ്ട്രിക്റ്റ് ഇക്വാലിറ്റി, വ്യത്യസ്ത ഡാറ്റാടൈപ്പുകൾ -> false \ No newline at end of file +7. സ്ട്രിക്റ്റ് ഇക്വാലിറ്റി, വ്യത്യസ്ത ഡാറ്റാടൈപ്പുകൾ -> false From 204bec02daaf2342f866b5c8235bf2141818e20b Mon Sep 17 00:00:00 2001 From: Sid <35549603+siddiqkaithodu@users.noreply.github.com> Date: Thu, 27 May 2021 10:52:54 +0530 Subject: [PATCH 15/16] Update solution.md --- .../09-comparison/1-comparison-questions/solution.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/02-first-steps/09-comparison/1-comparison-questions/solution.md b/1-js/02-first-steps/09-comparison/1-comparison-questions/solution.md index b29adaa51..d3331e4ec 100644 --- a/1-js/02-first-steps/09-comparison/1-comparison-questions/solution.md +++ b/1-js/02-first-steps/09-comparison/1-comparison-questions/solution.md @@ -17,5 +17,5 @@ null === +"\n0\n" → false 3. ഇതും ഡിക്ഷ്ണറി ഓർഡറിൽ താരതമ്യം ചെയ്യുന്നു, ആദ്യ അക്ഷരം `"2"`, ആദ്യ അക്ഷരം `"1"`നെക്കാൾ വലുതാണ്. 4. `null`,`undefined` വാല്യുകൾ അവയെ പരസ്പരം മാത്രം പൂർണമാക്കുന്നു(`==`). 5. സ്ട്രിക്റ്റ് ഇക്വാലിറ്റി ഓപറേറ്റർ സ്ട്രിക്റ്റ് ആണ്. വ്യത്യസ്ത ഡാറ്റാടൈപ്പുകൾ false റിട്ടേൺ ചെയ്യുന്നു. -6. `(4)`പോലെത്തന്നെ, `null`,`undefined` വാല്യുകൾ അവയെ പരസ്പരം മാത്രമേ പൂർണമാക്കുകയുളളൂ. +6. `(4)`പോലെത്തന്നെ, `null`,`undefined` വാല്യുകൾ പരസ്പരം തുല്യമാണ്. 7. സ്ട്രിക്റ്റ് ഇക്വാലിറ്റി, വ്യത്യസ്ത ഡാറ്റാടൈപ്പുകൾ -> false From 338b69a44bbeea66a71d988ce60214dc91eb8c5e Mon Sep 17 00:00:00 2001 From: Sid <35549603+siddiqkaithodu@users.noreply.github.com> Date: Thu, 27 May 2021 10:55:18 +0530 Subject: [PATCH 16/16] Update task.md --- .../02-first-steps/09-comparison/1-comparison-questions/task.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/02-first-steps/09-comparison/1-comparison-questions/task.md b/1-js/02-first-steps/09-comparison/1-comparison-questions/task.md index a31c5160c..df3c7a414 100644 --- a/1-js/02-first-steps/09-comparison/1-comparison-questions/task.md +++ b/1-js/02-first-steps/09-comparison/1-comparison-questions/task.md @@ -4,7 +4,7 @@ importance: 5 # Comparisons -ഇവയുടെ റിസൾട്ട് എന്തായിരിക്കും? +ഈ expressions ന്റെ റിസൾട്ട് എന്തായിരിക്കും? ```js no-beautify 5 > 4