Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions 1-js/05-data-types/04-array/1-item-value/solution.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
The result is `4`:
Kết quả là `4`:


```js run
let fruits = ["Apples", "Pear", "Orange"];
let fruits = ["Táo", "", "Cam"];

let shoppingCart = fruits;

shoppingCart.push("Banana");
shoppingCart.push("Chuối");

*!*
alert( fruits.length ); // 4
*/!*
```

That's because arrays are objects. So both `shoppingCart` and `fruits` are the references to the same array.

Đó là bởi vì array là đối tượng. Vì vậy, cả `shoppingCart` và `fruits` đều là các tham chiếu đến cùng một array.
13 changes: 6 additions & 7 deletions 1-js/05-data-types/04-array/1-item-value/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ importance: 3

---

# Is array copied?
# Array có được sao chép không?

What is this code going to show?
Mã này sẽ hiển thị là gì?

```js
let fruits = ["Apples", "Pear", "Orange"];
let fruits = ["Táo", "", "Cam"];

// push a new value into the "copy"
// đẩy một giá trị mới vào "bản sao"
let shoppingCart = fruits;
shoppingCart.push("Banana");
shoppingCart.push("Chuối");

// what's in fruits?
// có gì trong fruits?
alert( fruits.length ); // ?
```

44 changes: 22 additions & 22 deletions 1-js/05-data-types/04-array/10-maximal-subarray/solution.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
# Slow solution
# Giải pháp chậm

We can calculate all possible subsums.
Chúng ta có thể tính toán tất cả các tổng con có thể.

The simplest way is to take every element and calculate sums of all subarrays starting from it.
Cách đơn giản nhất là lấy mọi phần tử và tính tổng của tất cả các array con bắt đầu từ nó.

For instance, for `[-1, 2, 3, -9, 11]`:
Chẳng hạn, đối với `[-1, 2, 3, -9, 11]`:

```js no-beautify
// Starting from -1:
// Bắt đầu từ -1:
-1
-1 + 2
-1 + 2 + 3
-1 + 2 + 3 + (-9)
-1 + 2 + 3 + (-9) + 11

// Starting from 2:
// Bắt đầu từ 2:
2
2 + 3
2 + 3 + (-9)
2 + 3 + (-9) + 11

// Starting from 3:
// Bắt đầu từ 3:
3
3 + (-9)
3 + (-9) + 11

// Starting from -9
// Bắt đầu từ -9
-9
-9 + 11

// Starting from 11
// Bắt đầu từ 11
11
```

The code is actually a nested loop: the external loop over array elements, and the internal counts subsums starting with the current element.
Mã thực sự là một vòng lặp lồng nhau: vòng lặp bên ngoài trên các phần tử array và bên trong đếm các tổng con bắt đầu bằng phần tử hiện tại.

```js run
function getMaxSubSum(arr) {
let maxSum = 0; // if we take no elements, zero will be returned
let maxSum = 0; // nếu chúng ta không lấy phần tử nào, số 0 sẽ được trả về

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

The solution has a time complexity 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.
Giải pháp có độ phức tạp về thời gian là [O(n<sup>2</sup>)](https://vi.wikipedia.org/wiki/Kí_hiệu_O_lớn). Nói cách khác, nếu chúng ta tăng kích thước array lên 2 lần, thuật toán sẽ hoạt động lâu hơn 4 lần.

For big arrays (1000, 10000 or more items) such algorithms can lead to a serious sluggishness.
Đối với các array lớn (1000, 10000 mục trở lên), các thuật toán như vậy có thể dẫn đến tình trạng chậm chạp nghiêm trọng.

# Fast solution
# Giải pháp nhanh chóng

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.
Hãy di chuyển array và giữ tổng một phần hiện tại của các phần tử trong biến `s`. Nếu `s` trở thành số âm tại một thời điểm nào đó, thì hãy gán `s=0`. Tối đa của tất cả `s` như vậy sẽ là câu trả lời.

If the description is too vague, please see the code, it's short enough:
Nếu mô tả quá mơ hồ, hãy xem mã, nó đủ ngắn:

```js run demo
function getMaxSubSum(arr) {
let maxSum = 0;
let partialSum = 0;

for (let item of arr) { // for each item of arr
partialSum += item; // add it to partialSum
maxSum = Math.max(maxSum, partialSum); // remember the maximum
if (partialSum < 0) partialSum = 0; // zero if negative
for (let item of arr) { // cho mỗi mục của arr
partialSum += item; // thêm nó vào partialSum
maxSum = Math.max(maxSum, partialSum); // ghi nhớ số tối đa
if (partialSum < 0) partialSum = 0; // trả về không nếu âm
}

return maxSum;
Expand All @@ -89,6 +89,6 @@ alert( getMaxSubSum([1, 2, 3]) ); // 6
alert( getMaxSubSum([-1, -2, -3]) ); // 0
```

The algorithm requires exactly 1 array pass, so the time complexity is O(n).
Thuật toán yêu cầu chính xác 1 lần truyền array, vì vậy độ phức tạp về thời gian là O(n).

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.
Bạn có thể tìm thêm thông tin chi tiết về thuật toán tại đây: [Vấn đề array con tối đa](http://en.wikipedia.org/wiki/Maximum_subarray_problem). Nếu vẫn chưa rõ tại sao nó hoạt động, thì hãy theo dõi thuật toán trên các ví dụ trên, xem nó hoạt động như thế nào, điều đó tốt hơn bất kỳ từ nào.
18 changes: 9 additions & 9 deletions 1-js/05-data-types/04-array/10-maximal-subarray/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@ importance: 2

---

# A maximal subarray
# Một array con tối đa

The input is an array of numbers, e.g. `arr = [1, -2, 3, 4, -9, 6]`.
Đầu vào là một dãy số, ví dụ: `array = [1, -2, 3, 4, -9, 6]`.

The task is: find the contiguous subarray of `arr` with the maximal sum of items.
Nhiệm vụ là: tìm array con liền kề của `arr` với tổng các phần tử lớn nhất.

Write the function `getMaxSubSum(arr)` that will return that sum.
Viết hàm `getMaxSubSum(arr)` sẽ trả về tổng đó.

For instance:
Ví dụ:

```js
getMaxSubSum([-1, *!*2, 3*/!*, -9]) == 5 (the sum of highlighted items)
getMaxSubSum([-1, *!*2, 3*/!*, -9]) == 5 (tổng các mục được đánh dấu)
getMaxSubSum([*!*2, -1, 2, 3*/!*, -9]) == 6
getMaxSubSum([-1, 2, 3, -9, *!*11*/!*]) == 11
getMaxSubSum([-2, -1, *!*1, 2*/!*]) == 3
getMaxSubSum([*!*100*/!*, -9, 2, -3, 5]) == 100
getMaxSubSum([*!*1, 2, 3*/!*]) == 6 (take all)
getMaxSubSum([*!*1, 2, 3*/!*]) == 6 (lấy tất cả)
```

If all items are negative, it means that we take none (the subarray is empty), so the sum is zero:
Nếu tất cả các mục đều âm, điều đó có nghĩa là chúng ta không lấy gì (array con trống), vì vậy tổng bằng 0:

```js
getMaxSubSum([-1, -2, -3]) = 0
```

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.
Vui lòng thử nghĩ ra một giải pháp nhanh chóng: [O(n<sup>2</sup>)](https://vi.wikipedia.org/wiki/Kí_hiệu_O_lớn) hoặc thậm chí là O(n) nếu có thể.
16 changes: 8 additions & 8 deletions 1-js/05-data-types/04-array/2-create-array/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ importance: 5

---

# Array operations.
# Các phép toán array.

Let's try 5 array operations.
Hãy thử 5 phép toán array.

1. Create an array `styles` with items "Jazz" and "Blues".
2. Append "Rock-n-Roll" to the end.
3. Replace the value in the middle by "Classics". Your code for finding the middle value should work for any arrays with odd length.
4. Strip off the first value of the array and show it.
5. Prepend `Rap` and `Reggae` to the array.
1. Tạo một array `style` với các mục "Jazz" "Blues".
2. Nối "Rock-n-Roll" vào cuối.
3. Thay giá trị ở giữa với "Classics". Mã của bạn để tìm giá trị ở giữa sẽ hoạt động đối với bất kỳ array nào có độ dài lẻ.
4. Tách giá trị đầu tiên của array và hiển thị nó.
5. Thêm `Rap` `Reggae` vào array.

The array in the process:
Array trong quá trình:

```js no-beautify
Jazz, Blues
Expand Down
6 changes: 3 additions & 3 deletions 1-js/05-data-types/04-array/3-call-array-this/solution.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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`.
Lệnh gọi `arr[2]()` về mặt cú pháp là `obj[method]()` cũ và tốt, ở vai trò `obj` chúng ta có `arr` và ở vai trò `method` chúng ta có `2` .

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:
Vì vậy, chúng ta có một lệnh gọi hàm `arr[2]` như một phương thức đối tượng. Đương nhiên, nó nhận được `this` tham chiếu đến đối tượng `arr` và xuất ra array:

```js run
let arr = ["a", "b"];
Expand All @@ -12,4 +12,4 @@ arr.push(function() {
arr[2](); // a,b,function(){...}
```

The array has 3 values: initially it had two, plus the function.
Array có 3 giá trị: ban đầu nó có hai giá trị, cộng với hàm.
7 changes: 3 additions & 4 deletions 1-js/05-data-types/04-array/3-call-array-this/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ importance: 5

---

# Calling in an array context
# Gọi trong ngữ cảnh array

What is the result? Why?
Kết quả là gì? Tại sao?

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

arr.push(function() {
alert( this );
})
});

arr[2](); // ?
```

7 changes: 3 additions & 4 deletions 1-js/05-data-types/04-array/5-array-input-sum/solution.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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.
Hãy lưu ý chi tiết tinh tế nhưng quan trọng của giải pháp. Chúng ta không chuyển đổi `giá trị` thành số ngay sau `dấu nhắc`, bởi vì sau `giá trị = +giá trị`, chúng ta sẽ không thể phân biệt một chuỗi trống (dấu dừng) với số không (số hợp lệ). Thay vào đó, chúng ta làm điều đó sau.


```js run demo
Expand All @@ -8,9 +8,9 @@ function sumInput() {

while (true) {

let value = prompt("A number please?", 0);
let value = prompt("Xin vui lòng nhập một số?", 0);

// should we cancel?
// chúng ta có nên hủy không?
if (value === "" || value === null || !isFinite(value)) break;

numbers.push(+value);
Expand All @@ -25,4 +25,3 @@ function sumInput() {

alert( sumInput() );
```

12 changes: 6 additions & 6 deletions 1-js/05-data-types/04-array/5-array-input-sum/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ importance: 4

---

# Sum input numbers
# Tổng số đầu vào

Write the function `sumInput()` that:
Viết hàm `sumInput()` để:

- Asks the user for values using `prompt` and stores the values in the array.
- Finishes asking when the user enters a non-numeric value, an empty string, or presses "Cancel".
- Calculates and returns the sum of array items.
- Hỏi người dùng các giá trị bằng cách sử dụng `prompt` và lưu trữ các giá trị trong array.
- Kết thúc hỏi khi người dùng nhập giá trị không phải là số, chuỗi rỗng hoặc nhấn "Hủy".
- Tính toán và trả về tổng của các phần tử array.

P.S. A zero `0` is a valid number, please don't stop the input on zero.
Tái bút: Số không `0` là một số hợp lệ, vui lòng không dừng đầu vào ở số không.

[demo]
2 changes: 1 addition & 1 deletion 1-js/05-data-types/04-array/array-pop.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading