Skip to content

Commit 1eab285

Browse files
sunhaokkleviding
authored andcommitted
1 js/05 data types/05 array methods (#101)
* Update solution.md * Update solution.md * Update task.md * Update article.md * Update solution.md * Update article.md * Update task.md * Update task.md * Update task.md * Update task.md * Update task.md * Update solution.md * Update article.md * Update article.md * Update task.md * Update article.md * Update article.md * Update task.md * Update article.md * Update article.md * Update article.md * Update article.md * Update article.md * Update task.md * Update task.md * Update task.md * Update task.md * Update task.md * Update solution.md * Update task.md * Update task.md * Update task.md * Update solution.md * Update solution.md * Update task.md * Update task.md * Update solution.md * Update solution.md * Update solution.md * Update article.md * Update task.md * Update task.md * Update solution.md * Update task.md * Update article.md * Update article.md * Update solution.md * Update solution.md * Update task.md * 修正格式问题 * Update article.md * Update solution.md * Update task.md
1 parent 3d02e86 commit 1eab285

File tree

19 files changed

+290
-286
lines changed

19 files changed

+290
-286
lines changed

1-js/05-data-types/01-primitives-methods/1-string-new-property/solution.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ alert(str.test);
1010
```
1111

1212
这里有两种结果:
13+
1314
1. `undefined`
1415
2. 报错。
1516

1617
`(*)` 的那一行到底发生了什么呢:
1718

18-
1. 当访问 `str` 属性时,创建一个“包装对象”。
19+
1. 当访问 `str` 的属性时,创建一个“包装对象”。
1920
2. 当对属性进行操作的时候。这个对象获得了 `test` 属性。
2021
3. 操作结束,“包装对象”消失。
2122

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
JavaScript 允许我们像对象一样使用基本类型(字符串,数字等)。
44

5-
基本类型还提供调用方法等。我们会尽快研究这些,但首先我们会看看它是如何工作的,毕竟基本类型不是对象(在这里我们会更加清楚)。
5+
基本类型还提供调用方法等。我们会尽快研究这些,但首先我们会看看它是如何工作的,毕竟基本类型不是对象(在这里我们会分析的更加清楚)。
66

77
我们来看看基本类型和对象之间的关键区别。
88

@@ -27,11 +27,11 @@ john.sayHi(); // Hi buddy!
2727

2828
所以我们在这里创建了一个包含 `sayHi` 方法的对象 `john`
2929

30-
许多内置对象已经存在,例如那些与日期,错误,HTML 元素等一起工作的内置对象。它们具有不同的属性和方法。
30+
许多内置对象已经存在,例如那些处理日期,错误,HTML 元素等的内置对象。它们具有不同的属性和方法。
3131

3232
但是,这些特性都是有成本的!
3333

34-
对象比基本对象“更重”。他们需要额外的资源来支持运作。但是,由于属性和方法在编程中非常有用,JavaScript 引擎会尝试优化它们以减少额外的负担。
34+
对象比基本类型“更重”。他们需要额外的资源来支持运作。但是,由于属性和方法在编程中非常有用,JavaScript 引擎会尝试优化它们以减少额外的负担。
3535

3636
## 作为对象的基本类型
3737

@@ -79,7 +79,8 @@ alert( n.toFixed(2) ); // 1.23
7979
我们将在后面章节中看到更具体的方法 <info:number><info:string>
8080

8181

82-
````warn header="Constructors `String/Number/Boolean` are for internal use only"
82+
````warn header="构造函数 `String/Number/Boolean` 仅供内部使用"
83+
8384
像 Java 这样的一些语言允许我们使用 `new Number(1)``new Boolean(false)` 等语法明确地为基本类型创建“包装对象”。
8485

8586
在 JavaScript 中,由于历史原因,这也是可以的,但极其**不推荐**。因为这样会出问题。
@@ -106,13 +107,13 @@ if (zero) { // zero is true, because it's an object
106107

107108
例如,下面完全是有效的:
108109
```js
109-
let num = Number("123"); // convert a string to number
110+
let num = Number("123"); // 将字符串转成数字
110111
```
111112
````
112113
113114
114-
````warn header="null/undefined have no methods"
115-
特殊的基本类型 `null` 和 `undefined` 是个例外。他们没有相应的“包装对象”,并没有提供任何方法。从某种意义上说,他们是“最原始的”。
115+
````warn header="null/undefined 没有任何方法"
116+
特殊的基本类型 `null` 和 `undefined` 是个例外。他们没有相应的“包装对象”,也没有提供任何方法。从某种意义上说,他们是“最原始的”。
116117
117118
尝试访问这种值的属性会导致错误:
118119
@@ -123,4 +124,6 @@ alert(null.test); // error
123124
## 总结
124125

125126
-`null` 和 `undefined` 以外的基本类型都提供了许多有用的方法。我们将在即将到来的章节中研究这些内容。
126-
- 从形式上讲,这些方法通过临时对象工作,但 JavaScript 引擎可以很好地调整以优化内部,因此调用它们运行成本并不昂贵。
127+
128+
- 从形式上讲,这些方法通过临时对象工作,但 JavaScript 引擎可以很好地调整以优化内部,因此调用它们并不需要太高的成本。
129+

1-js/05-data-types/05-array-methods/1-camelcase/task.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ importance: 5
22

33
---
44

5-
# Translate border-left-width to borderLeftWidth
5+
# border-left-width 转换成 borderLeftWidth
66

7-
Write the function `camelize(str)` that changes dash-separated words like "my-short-string" into camel-cased "myShortString".
7+
写函数 `camelize(str)` 将诸如 "my-short-string" 之类的由短划线分隔的单词变成骆驼式的 "myShortString"
88

9-
That is: removes all dashes, each word after dash becomes uppercased.
9+
即:删除所有短横线,短横线后的第一个单词变为大写。
1010

11-
Examples:
11+
例如:
1212

1313
```js
1414
camelize("background-color") == 'backgroundColor';
1515
camelize("list-style-image") == 'listStyleImage';
1616
camelize("-webkit-transition") == 'WebkitTransition';
1717
```
1818

19-
P.S. Hint: use `split` to split the string into an array, transform it and `join` back.
19+
提示:使用 `split` 将字符串拆分成数组,然后将其转换 `join` 并返回。

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ importance: 4
22

33
---
44

5-
# Get average age
5+
# 获取平均
66

7-
Write the function `getAverageAge(users)` that gets an array of objects with property `age` and gets the average.
7+
编写 `getAverageAge(users)` 函数,该函数获取一个具有 age 属性的对象数组,并获取平均值。
88

9-
The formula for the average is `(age1 + age2 + ... + ageN) / N`.
9+
平均的公式是 `(age1 + age2 + ... + ageN) / N`
1010

11-
For instance:
11+
例如:
1212

1313
```js no-beautify
1414
let john = { name: "John", age: 25 };

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
Let's walk the array items:
2-
- For each item we'll check if the resulting array already has that item.
3-
- If it is so, then ignore, otherwise add to results.
1+
遍历数组
2+
- 对于每个元素,我们将检查结果数组是否已经有该元素。
3+
- 如果有,则忽略,否则添加结果。
44

55
```js run
66
function unique(arr) {
@@ -22,18 +22,18 @@ let strings = ["Hare", "Krishna", "Hare", "Krishna",
2222
alert( unique(strings) ); // Hare, Krishna, :-O
2323
```
2424

25-
The code works, but there's a potential performance problem in it.
25+
代码有效,但其中存在潜在的性能问题。
2626

27-
The method `result.includes(str)` internally walks the array `result` and compares each element against `str` to find the match.
27+
方法 `result.includes(str)` 在内部遍历数组 `result` 并将每个元素与 `str` 进行比较以找到匹配项。
2828

29-
So if there are `100` elements in `result` and no one matches `str`, then it will walk the whole `result` and do exactly `100` comparisons. And if `result` is large, like `10000`, then there would be `10000` comparisons.
29+
所以如果 `result` 中有 `100` 个元素,并且没有一个匹配上 `str`,那么它将遍历整个 `result` 并进行完全的 `100` 比较。如果 `result` 很大,比如 `10000`,那么会有 `10000` 次的比较。
3030

31-
That's not a problem by itself, because JavaScript engines are very fast, so walk `10000` array is a matter of microseconds.
31+
这本身并不是问题,因为 JavaScript 引擎速度非常快,所以遍历 10000 次就是几微秒的事。
3232

33-
But we do such test for each element of `arr`, in the `for` loop.
33+
但是我们在 `for `循环中为 `arr` 的每个元素做了这样的测试。
3434

35-
So if `arr.length` is `10000` we'll have something like `10000*10000` = 100 millions of comparisons. That's a lot.
35+
所以如果 `arr.length` `10000`,我们会有 `10000 * 10000` = 100 百万的比较。好多啊。
3636

37-
So the solution is only good for small arrays.
37+
所以该解决方案仅适用于小型数组。
3838

39-
Further in the chapter <info:map-set-weakmap-weakset> we'll see how to optimize it.
39+
进一步,在 <info:map-set-weakmap-weakset> 我们将看到如何优化它。

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ importance: 4
22

33
---
44

5-
# Filter unique array members
5+
# 数组去重
66

7-
Let `arr` be an array.
7+
`arr` 是一个数组
88

9-
Create a function `unique(arr)` that should return an array with unique items of `arr`.
9+
创建一个函数 `unique(arr)`,返回去除重复元素的 arr
1010

11-
For instance:
11+
例如:
1212

1313
```js
1414
function unique(arr) {

1-js/05-data-types/05-array-methods/2-filter-range/task.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ importance: 4
22

33
---
44

5-
# Filter range
5+
# 过滤范围
66

7-
Write a function `filterRange(arr, a, b)` that gets an array `arr`, looks for elements between `a` and `b` in it and returns an array of them.
7+
写一个函数 `filterRange(arr, a, b)` 获取一个数组 `arr`,查找 `a` `b` 之间的元素并返回它们的数组。
88

9-
The function should not modify the array. It should return the new array.
9+
该函数不应该修改原数组。它应该返回新的数组。
1010

11-
For instance:
11+
例如:
1212

1313
```js
1414
let arr = [5, 3, 8, 1];
1515

1616
let filtered = filterRange(arr, 1, 4);
1717

18-
alert( filtered ); // 3,1 (matching values)
18+
alert( filtered ); // 3,1(匹配值)
1919

20-
alert( arr ); // 5,3,8,1 (not modified)
20+
alert( arr ); // 5,3,8,1(未修改)
2121
```
2222

1-js/05-data-types/05-array-methods/3-filter-range-in-place/task.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ importance: 4
22

33
---
44

5-
# Filter range "in place"
5+
# 范围过滤
66

7-
Write a function `filterRangeInPlace(arr, a, b)` that gets an array `arr` and removes from it all values except those that are between `a` and `b`. The test is: `a ≤ arr[i] ≤ b`.
7+
写一个函数 `filterRangeInPlace(arr, a, b)` 获取一个数组 `arr`,并从中除去 `a` `b` 区间以外的所有值。测试:`a ≤ arr[i] ≤ b`
88

9-
The function should only modify the array. It should not return anything.
9+
该函数只应修改数组。它不应该返回任何东西。
1010

11-
For instance:
11+
例如:
1212
```js
1313
let arr = [5, 3, 8, 1];
1414

15-
filterRangeInPlace(arr, 1, 4); // removed the numbers except from 1 to 4
15+
filterRangeInPlace(arr, 1, 4); // 删除了从 1 到 4 之外的数字
1616

1717
alert( arr ); // [3, 1]
1818
```

1-js/05-data-types/05-array-methods/4-sort-back/task.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ importance: 4
22

33
---
44

5-
# Sort in the reverse order
5+
# 倒序
66

77
```js
88
let arr = [5, 2, 1, -10, 8];
99

10-
// ... your code to sort it in the reverse order
11-
10+
// ... 倒序
1211
alert( arr ); // 8, 5, 2, 1, -10
1312
```
1413

1-js/05-data-types/05-array-methods/5-copy-sort-array/solution.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
We can use `slice()` to make a copy and run the sort on it:
1+
我们可以使用 `slice()` 来创建一个副本并对其进行排序:
22

33
```js run
44
function copySorted(arr) {

0 commit comments

Comments
 (0)