Skip to content

Commit 0ca0cb8

Browse files
Arrays (#63)
* draft: article 50% * feat: translation of Arrays * Update 1-js/05-data-types/04-array/2-create-array/task.md Co-Authored-By: Jason Huang <[email protected]> * Apply suggestions from code review Co-Authored-By: Jason Huang <[email protected]>
1 parent 8404726 commit 0ca0cb8

File tree

11 files changed

+204
-208
lines changed

11 files changed

+204
-208
lines changed

1-js/05-data-types/04-array/1-item-value/solution.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
The result is `4`:
2-
1+
結果是 `4`
32

43
```js run
54
let fruits = ["Apples", "Pear", "Orange"];
@@ -13,5 +12,5 @@ alert( fruits.length ); // 4
1312
*/!*
1413
```
1514

16-
That's because arrays are objects. So both `shoppingCart` and `fruits` are the references to the same array.
15+
這是因為陣列是個物件,所以 `shoppingCart` `fruits` 這兩者皆是對同一個陣列的參考。
1716

1-js/05-data-types/04-array/1-item-value/task.md

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

33
---
44

5-
# Is array copied?
5+
# 陣列是否被複製?
66

7-
What is this code going to show?
7+
這段程式碼會顯示什麼?
88

99
```js
1010
let fruits = ["Apples", "Pear", "Orange"];
1111

12-
// push a new value into the "copy"
12+
// push 一個新的值到 "copy"
1313
let shoppingCart = fruits;
1414
shoppingCart.push("Banana");
1515

16-
// what's in fruits?
16+
// fruits 內會變成?
1717
alert( fruits.length ); // ?
1818
```
1919

1-js/05-data-types/04-array/10-maximal-subarray/solution.md

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
1-
# Slow solution
1+
# 慢的解法
22

3-
We can calculate all possible subsums.
3+
我們可以計算所有可能的子總和。
44

5-
The simplest way is to take every element and calculate sums of all subarrays starting from it.
5+
最簡單的方法是取出每個元素並計算所有由它開始的子陣列總和。
66

7-
For instance, for `[-1, 2, 3, -9, 11]`:
7+
舉個例,如 `[-1, 2, 3, -9, 11]`
88

99
```js no-beautify
10-
// Starting from -1:
10+
// 由 -1 開始:
1111
-1
1212
-1 + 2
1313
-1 + 2 + 3
1414
-1 + 2 + 3 + (-9)
1515
-1 + 2 + 3 + (-9) + 11
1616

17-
// Starting from 2:
17+
// 由 2 開始:
1818
2
1919
2 + 3
2020
2 + 3 + (-9)
2121
2 + 3 + (-9) + 11
2222

23-
// Starting from 3:
23+
// 由 3 開始:
2424
3
2525
3 + (-9)
2626
3 + (-9) + 11
2727

28-
// Starting from -9
28+
// 由 -9 開始:
2929
-9
3030
-9 + 11
3131

32-
// Starting from 11
32+
// 由 11 開始:
3333
11
3434
```
3535

36-
The code is actually a nested loop: the external loop over array elements, and the internal counts subsums starting with the current element.
36+
該程式碼事實上會是個巢狀迴圈:外部迴圈遍歷陣列元素,而內部的則計數由目前元素開始的子總和。
3737

3838
```js run
3939
function getMaxSubSum(arr) {
40-
let maxSum = 0; // if we take no elements, zero will be returned
40+
let maxSum = 0; // 若我們沒拿到元素,回傳零
4141

4242
for (let i = 0; i < arr.length; i++) {
4343
let sumFixedStart = 0;
@@ -57,25 +57,25 @@ alert( getMaxSubSum([1, 2, 3]) ); // 6
5757
alert( getMaxSubSum([100, -9, 2, -3, 5]) ); // 100
5858
```
5959

60-
The solution has a time complexety of [O(n<sup>2</sup>)](https://en.wikipedia.org/wiki/Big_O_notation). In other words, if we increase the array size 2 times, the algorithm will work 4 times longer.
60+
該解法的時間複雜度為 [O(n<sup>2</sup>)](https://en.wikipedia.org/wiki/Big_O_notation) 。換句話說,若我們增加兩倍的陣列大小,該演算法就會多花四倍的時間。
6161

62-
For big arrays (1000, 10000 or more items) such algorithms can lead to a serious sluggishness.
62+
對於大陣列來說(100010000 或更多項目)該演算法可能導致嚴重的延遲。
6363

64-
# Fast solution
64+
# 快的解法
6565

66-
Let's walk the array and keep the current partial sum of elements in the variable `s`. If `s` becomes negative at some point, then assign `s=0`. The maximum of all such `s` will be the answer.
66+
我們來遍歷陣列並在變數 `s` 中維持目前的元素部分總和。若 `s` 在某個時間點變成負值,那就指定 `s=0`。所有 `s` 中的最大值將會是答案。
6767

68-
If the description is too vague, please see the code, it's short enough:
68+
若這個描述太模糊了,請直接看程式碼,夠簡短了:
6969

7070
```js run demo
7171
function getMaxSubSum(arr) {
7272
let maxSum = 0;
7373
let partialSum = 0;
7474

75-
for (let item of arr) { // for each item of arr
76-
partialSum += item; // add it to partialSum
77-
maxSum = Math.max(maxSum, partialSum); // remember the maximum
78-
if (partialSum < 0) partialSum = 0; // zero if negative
75+
for (let item of arr) { // for..of arr 的每個 item
76+
partialSum += item; // 將它加入 partialSum
77+
maxSum = Math.max(maxSum, partialSum); // 記住最大值
78+
if (partialSum < 0) partialSum = 0; // 若為負則指定零
7979
}
8080

8181
return maxSum;
@@ -89,6 +89,7 @@ alert( getMaxSubSum([1, 2, 3]) ); // 6
8989
alert( getMaxSubSum([-1, -2, -3]) ); // 0
9090
```
9191

92-
The algorithm requires exactly 1 array pass, so the time complexity is O(n).
92+
該演算法只需要遍歷陣列恰好一次,所以時間複雜度為 O(n)。
93+
94+
你可以在這裡找到更多關於該演算法的細節資訊:[Maximum subarray problem](http://en.wikipedia.org/wiki/Maximum_subarray_problem)。若覺得對於它如何運作依然沒那麼明顯,請追蹤上述例子中的演算法來看看它是如何運作的,這麼做會比任何文字還要有用。
9395

94-
You can find more detail information about the algorithm here: [Maximum subarray problem](http://en.wikipedia.org/wiki/Maximum_subarray_problem). If it's still not obvious why that works, then please trace the algorithm on the examples above, see how it works, that's better than any words.

1-js/05-data-types/04-array/10-maximal-subarray/task.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,30 @@ importance: 2
22

33
---
44

5-
# A maximal subarray
5+
# 最大子陣列
66

7-
The input is an array of numbers, e.g. `arr = [1, -2, 3, 4, -9, 6]`.
7+
給予一個數值陣列,如 `arr = [1, -2, 3, 4, -9, 6]`
88

9-
The task is: find the contiguous subarray of `arr` with the maximal sum of items.
9+
此課題是:找出 `arr` 的某個項目連續的子陣列,其項目加總為最大。
1010

11-
Write the function `getMaxSubSum(arr)` that will return that sum.
11+
寫一個函式 `getMaxSubSum(arr)` 並回傳該加總值。
1212

13-
For instance:
13+
舉個例:
1414

1515
```js
16-
getMaxSubSum([-1, *!*2, 3*/!*, -9]) = 5 (the sum of highlighted items)
16+
getMaxSubSum([-1, *!*2, 3*/!*, -9]) = 5(被標示項目的加總)
1717
getMaxSubSum([*!*2, -1, 2, 3*/!*, -9]) = 6
1818
getMaxSubSum([-1, 2, 3, -9, *!*11*/!*]) = 11
1919
getMaxSubSum([-2, -1, *!*1, 2*/!*]) = 3
2020
getMaxSubSum([*!*100*/!*, -9, 2, -3, 5]) = 100
21-
getMaxSubSum([*!*1, 2, 3*/!*]) = 6 (take all)
21+
getMaxSubSum([*!*1, 2, 3*/!*]) = 6(全拿)
2222
```
2323

24-
If all items are negative, it means that we take none (the subarray is empty), so the sum is zero:
24+
若所有項目都是負值,代表我們不要拿任何東西(子陣列為空),所以加總為零:
2525

2626
```js
2727
getMaxSubSum([-1, -2, -3]) = 0
2828
```
2929

30-
Please try to think of a fast solution: [O(n<sup>2</sup>)](https://en.wikipedia.org/wiki/Big_O_notation) or even O(n) if you can.
30+
請試著思考快速的解法:[O(n<sup>2</sup>)](https://en.wikipedia.org/wiki/Big_O_notation) ,或者可以的話甚至可達 O(n)。
31+

1-js/05-data-types/04-array/2-create-array/solution.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

2-
32
```js run
43
let styles = ["Jazz", "Blues"];
54
styles.push("Rock-n-Roll");

1-js/05-data-types/04-array/2-create-array/task.md

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

33
---
44

5-
# Array operations.
5+
# 陣列操作
66

7-
Let's try 5 array operations.
7+
來試試 5 個陣列操作吧。
88

9-
1. Create an array `styles` with items "Jazz" and "Blues".
10-
2. Append "Rock-n-Roll" to the end.
11-
3. Replace the value in the middle by "Classics". Your code for finding the middle value should work for any arrays with odd length.
12-
4. Strip off the first value of the array and show it.
13-
5. Prepend `Rap` and `Reggae` to the array.
9+
1. 建立一個擁有 "Jazz" "Blues" 作為項目的陣列 `styles`
10+
2. 附加 "Rock-n-Roll" 到其末端。
11+
3. 使用 "Classics" 替換正中央的值,你寫的用於找出正中央值的程式碼,應該要能對任意奇數長度陣列運作。
12+
4. 抽離陣列第一個值並顯示它。
13+
5. 由前端附加 `Rap` `Reggae` 至陣列中。
1414

15-
The array in the process:
15+
這些過程中的陣列要長這樣:
1616

1717
```js no-beautify
1818
Jazz, Blues

1-js/05-data-types/04-array/3-call-array-this/solution.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
The call `arr[2]()` is syntactically the good old `obj[method]()`, in the role of `obj` we have `arr`, and in the role of `method` we have `2`.
1+
呼叫 `arr[2]()` 語法上和 `obj[method]()` 相同,對應 `obj` 的是 `arr`,而 `method` 對應著 `2`
22

3-
So we have a call of the function `arr[2]` as an object method. Naturally, it receives `this` referencing the object `arr` and outputs the array:
3+
所以我們有著視 `arr[2]` 為物件方法的函式呼叫。自然地,它接收參考至物件 `arr``this` 並輸出陣列:
44

55
```js run
66
let arr = ["a", "b"];
@@ -12,4 +12,5 @@ arr.push(function() {
1212
arr[2](); // "a","b",function
1313
```
1414

15-
The array has 3 values: initially it had two, plus the function.
15+
該陣列有三個值:一開始有兩個,再加上該函式。
16+

1-js/05-data-types/04-array/3-call-array-this/task.md

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

33
---
44

5-
# Calling in an array context
5+
# 於陣列上下文內呼叫
66

7-
What is the result? Why?
7+
這樣的結果是什麼?為什麼?
88

99
```js
1010
let arr = ["a", "b"];

1-js/05-data-types/04-array/5-array-input-sum/solution.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
Please note the subtle, but important detail of the solution. We don't convert `value` to number instantly after `prompt`, because after `value = +value` we would not be able to tell an empty string (stop sign) from the zero (valid number). We do it later instead.
2-
1+
請注意這個解法中微妙又重要的細節,我們不立刻在 `prompt` 之後轉換 `value` 為數值,因為在 `value = +value` 之後我們已經無法分辨空字串(停止的信號)和零(有效數值)。我們晚點再處理轉換這件事。
32

43
```js run demo
54
function sumInput() {
@@ -10,7 +9,7 @@ function sumInput() {
109

1110
let value = prompt("A number please?", 0);
1211

13-
// should we cancel?
12+
// 該終止了嗎?
1413
if (value === "" || value === null || !isFinite(value)) break;
1514

1615
numbers.push(+value);

1-js/05-data-types/04-array/5-array-input-sum/task.md

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

33
---
44

5-
# Sum input numbers
5+
# 加總輸入的數值
66

7-
Write the function `sumInput()` that:
7+
寫個函式 `SumInput()` 用於:
88

9-
- Asks the user for values using `prompt` and stores the values in the array.
10-
- Finishes asking when the user enters a non-numeric value, an empty string, or presses "Cancel".
11-
- Calculates and returns the sum of array items.
9+
- 使用 `prompt` 詢問使用者來輸入值,並儲存值到陣列中。
10+
- 當使用者輸入非數值的值、空字串或按下 "Cancel" 後結束詢問。
11+
- 計算並回傳陣列項目的加總。
1212

13-
P.S. A zero `0` is a valid number, please don't stop the input on zero.
13+
註:一個零 `0` 視為有效的數值,請不要在遇到零時停止詢問輸入。
1414

1515
[demo]

0 commit comments

Comments
 (0)