Skip to content

Commit b9bb854

Browse files
authored
Merge branch 'master' into patch-2
2 parents 48261eb + 492b6e1 commit b9bb854

File tree

84 files changed

+669
-650
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+669
-650
lines changed

1-js/02-first-steps/14-function-basics/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ There may be many occurrences of `return` in a single function. For instance:
266266
267267
```js run
268268
function checkAge(age) {
269-
if (age > 18) {
269+
if (age >= 18) {
270270
*!*
271271
return true;
272272
*/!*

1-js/03-code-quality/05-testing-mocha/article.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ We can select one of two ways to organize the test here:
159159
assert.equal(pow(2, 3), 8);
160160
});
161161
162-
it("3 raised to power 3 is 27", function() {
163-
assert.equal(pow(3, 3), 27);
162+
it("3 raised to power 4 is 81", function() {
163+
assert.equal(pow(3, 4), 81);
164164
});
165165
166166
});
@@ -182,7 +182,7 @@ The result:
182182

183183
[iframe height=250 src="pow-2" edit border="1"]
184184

185-
As we could expect, the second test failed. Sure, our function always returns `8`, while the `assert` expects `27`.
185+
As we could expect, the second test failed. Sure, our function always returns `8`, while the `assert` expects `81`.
186186

187187
## Improving the implementation
188188

1-js/03-code-quality/05-testing-mocha/pow-2.view/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ describe("pow", function() {
44
assert.equal(pow(2, 3), 8);
55
});
66

7-
it("3 raised to power 3 is 27", function() {
8-
assert.equal(pow(3, 3), 27);
7+
it("3 raised to power 4 is 81", function() {
8+
assert.equal(pow(3, 4), 81);
99
});
1010

1111
});

1-js/05-data-types/01-primitives-methods/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Let's look at the key distinctions between primitives and objects.
77
A primitive
88

99
- Is a value of a primitive type.
10-
- There are 6 primitive types: `string`, `number`, `boolean`, `symbol`, `null` and `undefined`.
10+
- There are 7 primitive types: `string`, `number`, `bigint`, `boolean`, `symbol`, `null` and `undefined`.
1111

1212
An object
1313

1-js/05-data-types/02-number/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ There are more functions and constants in `Math` object, including trigonometry,
413413
To write numbers with many zeroes:
414414

415415
- Append `"e"` with the zeroes count to the number. Like: `123e6` is the same as `123` with 6 zeroes `123000000`.
416-
- A negative number after `"e"` causes the number to be divided by 1 with given zeroes. E.g. `123e-6` means `0.000123` (`123` millionth).
416+
- A negative number after `"e"` causes the number to be divided by 1 with given zeroes. E.g. `123e-6` means `0.000123` (`123` millionths).
417417

418418
For different numeral systems:
419419

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function groupById(array) {
2+
return array.reduce((obj, value) => {
3+
obj[value.id] = value;
4+
return obj;
5+
}, {})
6+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
describe("groupById", function() {
2+
3+
it("creates an object grouped by id", function() {
4+
let users = [
5+
{id: 'john', name: "John Smith", age: 20},
6+
{id: 'ann', name: "Ann Smith", age: 24},
7+
{id: 'pete', name: "Pete Peterson", age: 31},
8+
];
9+
10+
assert.deepEqual(groupById(users), {
11+
john: {id: 'john', name: "John Smith", age: 20}
12+
ann: {id: 'ann', name: "Ann Smith", age: 24},
13+
pete: {id: 'pete', name: "Pete Peterson", age: 31},
14+
});
15+
});
16+
17+
it("works with an empty array", function() {
18+
assert.deepEqual(groupById(users), {});
19+
});
20+
});

1-js/05-data-types/05-array-methods/12-reduce-object/solution.md

Whitespace-only changes.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
importance: 4
2+
3+
---
4+
5+
# Create keyed object from array
6+
7+
Let's say we received an array of users in the form `{id:..., name:..., age... }`.
8+
9+
Create a function `groupById(arr)` that creates an object from it, with `id` as the key, and array items as values.
10+
11+
For example:
12+
13+
```js
14+
let users = [
15+
{id: 'john', name: "John Smith", age: 20},
16+
{id: 'ann', name: "Ann Smith", age: 24},
17+
{id: 'pete', name: "Pete Peterson", age: 31},
18+
];
19+
20+
let usersById = groupById(users);
21+
22+
/*
23+
// after the call we have:
24+
25+
usersById = {
26+
john: {id: 'john', name: "John Smith", age: 20}
27+
ann: {id: 'ann', name: "Ann Smith", age: 24},
28+
pete: {id: 'pete', name: "Pete Peterson", age: 31},
29+
}
30+
*/
31+
```
32+
33+
Such function is really handy when working with server data.
34+
35+
In this task we assume that `id` is unique. There may be no two array items with the same `id`.
36+
37+
Please use array `.reduce` method in the solution.

1-js/05-data-types/05-array-methods/article.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ The order became `1, 15, 2`. Incorrect. But why?
384384

385385
**The items are sorted as strings by default.**
386386

387-
Literally, all elements are converted to strings for comparisons. For strings, lexicographic ordering is applied and indeed `"2" > "15"`.
387+
Literally, all elements are converted to strings for comparisons. For strings, lexicographic ordering is applied and indeed `"2" > "15"`.
388388

389389
To use our own sorting order, we need to supply a function as the argument of `arr.sort()`.
390390

@@ -431,7 +431,6 @@ By the way, if we ever want to know which elements are compared -- nothing preve
431431

432432
The algorithm may compare an element with multiple others in the process, but it tries to make as few comparisons as possible.
433433

434-
435434
````smart header="A comparison function may return any number"
436435
Actually, a comparison function is only required to return a positive number to say "greater" and a negative number to say "less".
437436
@@ -456,6 +455,22 @@ arr.sort( (a, b) => a - b );
456455
This works exactly the same as the longer version above.
457456
````
458457

458+
````smart header="Use `localeCompare` for strings"
459+
Remember [strings](info:string#correct-comparisons) comparison algorithm? It compares letters by their codes by default.
460+
461+
For many alphabets, it's better to use `str.localeCompare` method to correctly sort letters, such as `Ö`.
462+
463+
For example, let's sort a few countries in German:
464+
465+
```js run
466+
let countries = ['Österreich', 'Andorra', 'Vietnam'];
467+
468+
alert( countries.sort( (a, b) => a > b ? 1 : -1) ); // Andorra, Vietnam, Österreich (wrong)
469+
470+
alert( countries.sort( (a, b) => a.localeCompare(b) ) ); // Andorra,Österreich,Vietnam (correct!)
471+
```
472+
````
473+
459474
### reverse
460475
461476
The method [arr.reverse](mdn:js/Array/reverse) reverses the order of elements in `arr`.
@@ -530,7 +545,7 @@ The methods [arr.reduce](mdn:js/Array/reduce) and [arr.reduceRight](mdn:js/Array
530545
The syntax is:
531546

532547
```js
533-
let value = arr.reduce(function(previousValue, item, index, array) {
548+
let value = arr.reduce(function(accumulator, item, index, array) {
534549
// ...
535550
}, [initial]);
536551
```
@@ -539,14 +554,16 @@ The function is applied to all array elements one after another and "carries on"
539554

540555
Arguments:
541556

542-
- `previousValue` -- is the result of the previous function call, equals `initial` the first time (if `initial` is provided).
557+
- `accumulator` -- is the result of the previous function call, equals `initial` the first time (if `initial` is provided).
543558
- `item` -- is the current array item.
544559
- `index` -- is its position.
545560
- `array` -- is the array.
546561

547562
As function is applied, the result of the previous function call is passed to the next one as the first argument.
548563

549-
Sounds complicated, but it's not if you think about the first argument as the "accumulator" that stores the combined result of all previous execution. And at the end it becomes the result of `reduce`.
564+
So, the first argument is essentially the accumulator that stores the combined result of all previous executions. And at the end it becomes the result of `reduce`.
565+
566+
Sounds complicated?
550567

551568
The easiest way to grasp that is by example.
552569

@@ -611,7 +628,6 @@ let arr = [];
611628
arr.reduce((sum, current) => sum + current);
612629
```
613630

614-
615631
So it's advised to always specify the initial value.
616632

617633
The method [arr.reduceRight](mdn:js/Array/reduceRight) does the same, but goes from right to left.

0 commit comments

Comments
 (0)