diff --git a/1-js/02-first-steps/06-type-conversions/1-primitive-conversions-questions/solution.md b/1-js/02-first-steps/06-type-conversions/1-primitive-conversions-questions/solution.md index a4f3fec7..ce48cbff 100644 --- a/1-js/02-first-steps/06-type-conversions/1-primitive-conversions-questions/solution.md +++ b/1-js/02-first-steps/06-type-conversions/1-primitive-conversions-questions/solution.md @@ -17,6 +17,6 @@ undefined + 1 = NaN // (4) ``` 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. -2. The subtraction `"-"` (like most math operations) only works with numbers, it converts an empty string `""` to `0`. +2. The subtraction `-` (like most math operations) only works with numbers, it converts an empty string `""` to `0`. 3. `null` becomes `0` after the numeric conversion. 4. `undefined` becomes `NaN` after the numeric conversion. diff --git a/1-js/02-first-steps/07-operators/article.md b/1-js/02-first-steps/07-operators/article.md index df24311c..17a807af 100644 --- a/1-js/02-first-steps/07-operators/article.md +++ b/1-js/02-first-steps/07-operators/article.md @@ -11,7 +11,7 @@ In this chapter we concentrate on aspects that are not covered by school arithme Before we move on, let's grasp the common terminology. - *An operand* -- is what operators are applied to. For instance in multiplication `5 * 2` there are two operands: the left operand is `5`, and the right operand is `2`. Sometimes people say "arguments" instead of "operands". -- An operator is *unary* if it has a single operand. For example, the unary negation `"-"` reverses the sign of the number: +- An operator is *unary* if it has a single operand. For example, the unary negation `-` reverses the sign of the number: ```js run let x = 1; @@ -34,7 +34,7 @@ Before we move on, let's grasp the common terminology. Now let's see special features of JavaScript operators that are beyond school arithmetics. -Usually the plus operator `'+'` sums numbers. +Usually the plus operator `+` sums numbers. But if the binary `+` is applied to strings, it merges (concatenates) them: @@ -43,7 +43,7 @@ let s = "my" + "string"; alert(s); // mystring ``` -Note that if any of operands is a string, then the other one is converted to a string too. +Note that if any of the operands is a string, then the other one is converted to a string too. For example: @@ -54,7 +54,14 @@ alert( 2 + '1' ); // "21" See, it doesn't matter whether the first operand is a string or the second one. The rule is simple: if either operand is a string, then convert the other one into a string as well. -String concatenation and conversion is a special feature of the binary plus `"+"`. Other arithmetic operators work only with numbers. They always convert their operands to numbers. +However, note that operations run from left to right. If there are two numbers followed by a string, the numbers will be added before being converted to a string: + + +```js run +alert(2 + 2 + '1' ); // "41" and not "221" +``` + +String concatenation and conversion is a special feature of the binary plus `+`. Other arithmetic operators work only with numbers. They always convert their operands to numbers. For instance, subtraction and division: @@ -65,7 +72,7 @@ alert( '6' / '2' ); // 3 ## Numeric conversion, unary + -The plus `+` exist in two forms. The binary form that we used above and the unary form. +The plus `+` exists in two forms. The binary form that we used above and the unary form. The unary plus or, in other words, the plus operator `+` applied to a single value, doesn't do anything with numbers, but if the operand is not a number, then it is converted into it. @@ -86,9 +93,9 @@ alert( +"" ); // 0 */!* ``` -It actually does the same as `Number(...)`, but shorter. +It actually does the same as `Number(...)`, but is shorter. -A need to convert string to number arises very often. For example, if we are getting values from HTML form fields, then they are usually strings. +A need to convert strings to numbers arises very often. For example, if we are getting values from HTML form fields, then they are usually strings. What if we want to sum them? @@ -246,14 +253,14 @@ So, there are special operators for that: ```js run no-beautify let counter = 2; - counter++; // works same as counter = counter + 1, but shorter + counter++; // works the same as counter = counter + 1, but is shorter alert( counter ); // 3 ``` - **Decrement** `--` decreases a variable by 1: ```js run no-beautify let counter = 2; - counter--; // works same as counter = counter - 1, but shorter + counter--; // works the same as counter = counter - 1, but is shorter alert( counter ); // 1 ``` @@ -401,9 +408,9 @@ alert( n ); // 16 (right part evaluated first, same as n *= 8) ## Comma -The comma operator `','` is one of most rare and unusual operators. Sometimes it's used to write shorter code, so we need to know it in order to understand what's going on. +The comma operator `,` is one of most rare and unusual operators. Sometimes it's used to write shorter code, so we need to know it in order to understand what's going on. -The comma operator allows us to evaluate several expressions, dividing them with a comma `','`. Each of them is evaluated, but the result of only the last one is returned. +The comma operator allows us to evaluate several expressions, dividing them with a comma `,`. Each of them is evaluated, but the result of only the last one is returned. For example: diff --git a/1-js/05-data-types/02-number/8-random-min-max/task.md b/1-js/05-data-types/02-number/8-random-min-max/task.md index cde399db..7037cfcb 100644 --- a/1-js/05-data-types/02-number/8-random-min-max/task.md +++ b/1-js/05-data-types/02-number/8-random-min-max/task.md @@ -15,5 +15,3 @@ alert( random(1, 5) ); // 1.2345623452 alert( random(1, 5) ); // 3.7894332423 alert( random(1, 5) ); // 4.3435234525 ``` - -You can use the solution of the [previous task](info:task/random-min-max) as the base. \ No newline at end of file diff --git a/1-js/05-data-types/02-number/9-random-int-min-max/solution.md b/1-js/05-data-types/02-number/9-random-int-min-max/solution.md index 1b007032..0950ff81 100644 --- a/1-js/05-data-types/02-number/9-random-int-min-max/solution.md +++ b/1-js/05-data-types/02-number/9-random-int-min-max/solution.md @@ -27,7 +27,7 @@ Now we can clearly see that `1` gets twice less values than `2`. And the same wi # The correct solution -There are many correct solutions to the task. One of them is to adjust interval borders. To ensure the same intervals, we can generate values from `0.5 to 2.5`, thus adding the required probabilities to the edges: +There are many correct solutions to the task. One of them is to adjust interval borders. To ensure the same intervals, we can generate values from `0.5 to 3.5`, thus adding the required probabilities to the edges: ```js run *!* diff --git a/1-js/05-data-types/02-number/9-random-int-min-max/task.md b/1-js/05-data-types/02-number/9-random-int-min-max/task.md index 71d83f01..29341b2a 100644 --- a/1-js/05-data-types/02-number/9-random-int-min-max/task.md +++ b/1-js/05-data-types/02-number/9-random-int-min-max/task.md @@ -15,4 +15,6 @@ Examples of its work: alert( random(1, 5) ); // 1 alert( random(1, 5) ); // 3 alert( random(1, 5) ); // 5 -``` \ No newline at end of file +``` + +You can use the solution of the [previous task](info:task/random-min-max) as the base.