Skip to content

Commit b1031a6

Browse files
committed
Update "Array methods" English files
1 parent 87d2b8e commit b1031a6

File tree

5 files changed

+50
-18
lines changed

5 files changed

+50
-18
lines changed

1-js/05-data-types/05-array-methods/10-average-age/task.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ importance: 4
44

55
# Get average age
66

7-
Write the function `getAverageAge(users)` that gets an array of objects with property `age` and gets the average.
7+
Write the function `getAverageAge(users)` that gets an array of objects with property `age` and returns the average age.
88

99
The formula for the average is `(age1 + age2 + ... + ageN) / N`.
1010

@@ -19,4 +19,3 @@ let arr = [ john, pete, mary ];
1919

2020
alert( getAverageAge(arr) ); // (25 + 30 + 29) / 3 = 28
2121
```
22-

1-js/05-data-types/05-array-methods/11-array-unique/solution.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ So if `arr.length` is `10000` we'll have something like `10000*10000` = 100 mill
3636

3737
So the solution is only good for small arrays.
3838

39-
Further in the chapter <info:map-set-weakmap-weakset> we'll see how to optimize it.
39+
Further in the chapter <info:map-set> we'll see how to optimize it.

1-js/05-data-types/05-array-methods/3-filter-range-in-place/_js.view/test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ describe("filterRangeInPlace", function() {
44

55
let arr = [5, 3, 8, 1];
66

7-
filterRangeInPlace(arr, 1, 4);
7+
filterRangeInPlace(arr, 2, 5);
88

9-
assert.deepEqual(arr, [3, 1]);
9+
assert.deepEqual(arr, [5, 3]);
1010
});
1111

1212
it("doesn't return anything", function() {
1313
assert.isUndefined(filterRangeInPlace([1,2,3], 1, 4));
1414
});
1515

16-
});
16+
});

1-js/05-data-types/05-array-methods/9-shuffle/solution.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ for (let key in count) {
4545
}
4646
```
4747

48-
An example result (for V8, July 2017):
48+
An example result (depends on JS engine):
4949

5050
```js
5151
123: 250706

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

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,13 @@ Now let's cover methods that search in an array.
234234

235235
### indexOf/lastIndexOf and includes
236236

237-
The methods [arr.indexOf](mdn:js/Array/indexOf), [arr.lastIndexOf](mdn:js/Array/lastIndexOf) and [arr.includes](mdn:js/Array/includes) have the same syntax and do essentially the same as their string counterparts, but operate on items instead of characters:
237+
The methods [arr.indexOf](mdn:js/Array/indexOf) and [arr.includes](mdn:js/Array/includes) have the similar syntax and do essentially the same as their string counterparts, but operate on items instead of characters:
238238

239239
- `arr.indexOf(item, from)` -- looks for `item` starting from index `from`, and returns the index where it was found, otherwise `-1`.
240-
- `arr.lastIndexOf(item, from)` -- same, but looks for from right to left.
241240
- `arr.includes(item, from)` -- looks for `item` starting from index `from`, returns `true` if found.
242241

242+
Usually these methods are used with only one argument: the `item` to search. By default, the search is from the beginning.
243+
243244
For instance:
244245

245246
```js run
@@ -252,19 +253,31 @@ alert( arr.indexOf(null) ); // -1
252253
alert( arr.includes(1) ); // true
253254
```
254255

255-
Note that the methods use `===` comparison. So, if we look for `false`, it finds exactly `false` and not the zero.
256+
Please note that `indexOf` uses the strict equality `===` for comparison. So, if we look for `false`, it finds exactly `false` and not the zero.
257+
258+
If we want to check if `item` exists in the array, and don't need the index, then `arr.includes` is preferred.
259+
260+
The method [arr.lastIndexOf](mdn:js/Array/lastIndexOf) is the same as `indexOf`, but looks for from right to left.
256261

257-
If we want to check for inclusion, and don't want to know the exact index, then `arr.includes` is preferred.
262+
```js run
263+
let fruits = ['Apple', 'Orange', 'Apple']
264+
265+
alert( fruits.indexOf('Apple') ); // 0 (first Apple)
266+
alert( fruits.lastIndexOf('Apple') ); // 2 (last Apple)
267+
```
258268

259-
Also, a very minor difference of `includes` is that it correctly handles `NaN`, unlike `indexOf/lastIndexOf`:
269+
````smart header="The `includes` method handles `NaN` correctly"
270+
A minor, but noteworthy feature of `includes` is that it correctly handles `NaN`, unlike `indexOf`:
260271

261272
```js run
262273
const arr = [NaN];
263-
alert( arr.indexOf(NaN) ); // -1 (should be 0, but === equality doesn't work for NaN)
274+
alert( arr.indexOf(NaN) ); // -1 (wrong, should be 0)
264275
alert( arr.includes(NaN) );// true (correct)
265276
```
277+
That's because `includes` was added to JavaScript much later and uses the more up to date comparison algorithm internally.
278+
````
266279
267-
### find and findIndex
280+
### find and findIndex/findLastIndex
268281
269282
Imagine we have an array of objects. How do we find an object with the specific condition?
270283
@@ -304,7 +317,26 @@ In real life arrays of objects is a common thing, so the `find` method is very u
304317
305318
Note that in the example we provide to `find` the function `item => item.id == 1` with one argument. That's typical, other arguments of this function are rarely used.
306319
307-
The [arr.findIndex](mdn:js/Array/findIndex) method is essentially the same, but it returns the index where the element was found instead of the element itself and `-1` is returned when nothing is found.
320+
The [arr.findIndex](mdn:js/Array/findIndex) method has the same syntax, but returns the index where the element was found instead of the element itself. The value of `-1` is returned if nothing is found.
321+
322+
The [arr.findLastIndex](mdn:js/Array/findLastIndex) method is like `findIndex`, but searches from right to left, similar to `lastIndexOf`.
323+
324+
Here's an example:
325+
326+
```js run
327+
let users = [
328+
{id: 1, name: "John"},
329+
{id: 2, name: "Pete"},
330+
{id: 3, name: "Mary"},
331+
{id: 4, name: "John"}
332+
];
333+
334+
// Find the index of the first John
335+
alert(users.findIndex(user => user.name == 'John')); // 0
336+
337+
// Find the index of the last John
338+
alert(users.findLastIndex(user => user.name == 'John')); // 3
339+
```
308340
309341
### filter
310342
@@ -389,6 +421,7 @@ Literally, all elements are converted to strings for comparisons. For strings, l
389421
To use our own sorting order, we need to supply a function as the argument of `arr.sort()`.
390422
391423
The function should compare two arbitrary values and return:
424+
392425
```js
393426
function compare(a, b) {
394427
if (a > b) return 1; // if the first value is greater than the second
@@ -633,7 +666,6 @@ So it's advised to always specify the initial value.
633666

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

636-
637669
## Array.isArray
638670

639671
Arrays do not form a separate language type. They are based on objects.
@@ -642,7 +674,7 @@ So `typeof` does not help to distinguish a plain object from an array:
642674

643675
```js run
644676
alert(typeof {}); // object
645-
alert(typeof []); // same
677+
alert(typeof []); // object (same)
646678
```
647679

648680
...But arrays are used so often that there's a special method for that: [Array.isArray(value)](mdn:js/Array/isArray). It returns `true` if the `value` is an array, and `false` otherwise.
@@ -733,7 +765,7 @@ A cheat sheet of array methods:
733765
- `reduce/reduceRight(func, initial)` -- calculate a single value over the array by calling `func` for each element and passing an intermediate result between the calls.
734766

735767
- Additionally:
736-
- `Array.isArray(arr)` checks `arr` for being an array.
768+
- `Array.isArray(value)` checks `value` for being an array, if so returns `true`, otherwise `false`.
737769

738770
Please note that methods `sort`, `reverse` and `splice` modify the array itself.
739771

@@ -746,6 +778,7 @@ These methods are the most used ones, they cover 99% of use cases. But there are
746778
These methods behave sort of like `||` and `&&` operators: if `fn` returns a truthy value, `arr.some()` immediately returns `true` and stops iterating over the rest of items; if `fn` returns a falsy value, `arr.every()` immediately returns `false` and stops iterating over the rest of items as well.
747779

748780
We can use `every` to compare arrays:
781+
749782
```js run
750783
function arraysEqual(arr1, arr2) {
751784
return arr1.length === arr2.length && arr1.every((value, index) => value === arr2[index]);

0 commit comments

Comments
 (0)