Skip to content

Commit c706585

Browse files
authored
Merge pull request #359 from brentguf/numbers-task-6-solution
Numbers task 6 solution
2 parents 662c389 + 7f8b27d commit c706585

File tree

5 files changed

+23
-16
lines changed

5 files changed

+23
-16
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ undefined + 1 = NaN // (4)
1717
```
1818

1919
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.
20-
2. The subtraction `"-"` (like most math operations) only works with numbers, it converts an empty string `""` to `0`.
20+
2. The subtraction `-` (like most math operations) only works with numbers, it converts an empty string `""` to `0`.
2121
3. `null` becomes `0` after the numeric conversion.
2222
4. `undefined` becomes `NaN` after the numeric conversion.

1-js/02-first-steps/07-operators/article.md

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ In this chapter we concentrate on aspects that are not covered by school arithme
1111
Before we move on, let's grasp the common terminology.
1212

1313
- *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".
14-
- An operator is *unary* if it has a single operand. For example, the unary negation `"-"` reverses the sign of the number:
14+
- An operator is *unary* if it has a single operand. For example, the unary negation `-` reverses the sign of the number:
1515

1616
```js run
1717
let x = 1;
@@ -34,7 +34,7 @@ Before we move on, let's grasp the common terminology.
3434
3535
Now let's see special features of JavaScript operators that are beyond school arithmetics.
3636

37-
Usually the plus operator `'+'` sums numbers.
37+
Usually the plus operator `+` sums numbers.
3838

3939
But if the binary `+` is applied to strings, it merges (concatenates) them:
4040

@@ -43,7 +43,7 @@ let s = "my" + "string";
4343
alert(s); // mystring
4444
```
4545

46-
Note that if any of operands is a string, then the other one is converted to a string too.
46+
Note that if any of the operands is a string, then the other one is converted to a string too.
4747

4848
For example:
4949

@@ -54,7 +54,14 @@ alert( 2 + '1' ); // "21"
5454

5555
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.
5656
57-
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.
57+
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:
58+
59+
60+
```js run
61+
alert(2 + 2 + '1' ); // "41" and not "221"
62+
```
63+
64+
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.
5865
5966
For instance, subtraction and division:
6067
@@ -65,7 +72,7 @@ alert( '6' / '2' ); // 3
6572
6673
## Numeric conversion, unary +
6774
68-
The plus `+` exist in two forms. The binary form that we used above and the unary form.
75+
The plus `+` exists in two forms. The binary form that we used above and the unary form.
6976
7077
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.
7178

@@ -86,9 +93,9 @@ alert( +"" ); // 0
8693
*/!*
8794
```
8895

89-
It actually does the same as `Number(...)`, but shorter.
96+
It actually does the same as `Number(...)`, but is shorter.
9097

91-
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.
98+
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.
9299

93100
What if we want to sum them?
94101

@@ -246,14 +253,14 @@ So, there are special operators for that:
246253
247254
```js run no-beautify
248255
let counter = 2;
249-
counter++; // works same as counter = counter + 1, but shorter
256+
counter++; // works the same as counter = counter + 1, but is shorter
250257
alert( counter ); // 3
251258
```
252259
- **Decrement** `--` decreases a variable by 1:
253260
254261
```js run no-beautify
255262
let counter = 2;
256-
counter--; // works same as counter = counter - 1, but shorter
263+
counter--; // works the same as counter = counter - 1, but is shorter
257264
alert( counter ); // 1
258265
```
259266
@@ -401,9 +408,9 @@ alert( n ); // 16 (right part evaluated first, same as n *= 8)
401408
402409
## Comma
403410
404-
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.
411+
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.
405412
406-
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.
413+
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.
407414
408415
For example:
409416

1-js/05-data-types/02-number/8-random-min-max/task.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,3 @@ alert( random(1, 5) ); // 1.2345623452
1515
alert( random(1, 5) ); // 3.7894332423
1616
alert( random(1, 5) ); // 4.3435234525
1717
```
18-
19-
You can use the solution of the [previous task](info:task/random-min-max) as the base.

1-js/05-data-types/02-number/9-random-int-min-max/solution.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Now we can clearly see that `1` gets twice less values than `2`. And the same wi
2727

2828
# The correct solution
2929

30-
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:
30+
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:
3131

3232
```js run
3333
*!*

1-js/05-data-types/02-number/9-random-int-min-max/task.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ Examples of its work:
1515
alert( random(1, 5) ); // 1
1616
alert( random(1, 5) ); // 3
1717
alert( random(1, 5) ); // 5
18-
```
18+
```
19+
20+
You can use the solution of the [previous task](info:task/random-min-max) as the base.

0 commit comments

Comments
 (0)