Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
dd7d046
Update article.md
ImVietnam Mar 6, 2023
8aefb00
Update article.md
ImVietnam Mar 6, 2023
154a341
Update article.md
ImVietnam Mar 6, 2023
74d01f6
Update task.md
ImVietnam Mar 6, 2023
8d1dd28
Update solution.js
ImVietnam Mar 6, 2023
e1c5408
Update task.md
ImVietnam Mar 6, 2023
570ffd2
Update task.md
ImVietnam Mar 6, 2023
f5be4a5
Update solution.md
ImVietnam Mar 6, 2023
1f66e69
Update task.md
ImVietnam Mar 6, 2023
a4b0a48
Update task.md
ImVietnam Mar 6, 2023
4762dc8
Update solution.md
ImVietnam Mar 6, 2023
5629024
Update solution.js
ImVietnam Mar 6, 2023
31fabf5
Update task.md
ImVietnam Mar 6, 2023
724946c
Update solution.md
ImVietnam Mar 6, 2023
bb2f8cf
Update solution.js
ImVietnam Mar 6, 2023
d7d8528
Update task.md
ImVietnam Mar 6, 2023
bf41736
Update task.md
ImVietnam Mar 6, 2023
8b8c307
Update solution.md
ImVietnam Mar 6, 2023
ad2f1f5
Update task.md
ImVietnam Mar 6, 2023
7d23eb6
Update task.md
ImVietnam Mar 7, 2023
8f4520c
Update solution.md
ImVietnam Mar 7, 2023
30810b4
Update task.md
ImVietnam Mar 7, 2023
686a04e
Update task.md
ImVietnam Mar 7, 2023
b86142f
Update solution.md
ImVietnam Mar 7, 2023
002962c
Update task.md
ImVietnam Mar 7, 2023
5e38d92
Update solution.md
ImVietnam Mar 7, 2023
8128e42
Update task.md
ImVietnam Mar 7, 2023
5ef0f45
Update solution.md
ImVietnam Mar 7, 2023
f2b2696
Merge branch 'javascript-tutorial:master' into patch-15
ImVietnam Jun 9, 2023
6ef4172
Update solution.js
ImVietnam Jun 12, 2023
d1ac0c3
Update task.md
ImVietnam Jun 12, 2023
8d8fd22
Update solution.js
ImVietnam Jun 12, 2023
e60ffbd
some fixes
ImVietnam Jun 12, 2023
65a4e4a
Update task.md
ImVietnam Jun 12, 2023
d21bf9e
Update solution.js
ImVietnam Jun 12, 2023
ea02aaa
Update task.md
ImVietnam Jun 12, 2023
8928002
Update solution.md
ImVietnam Jun 12, 2023
3ad8efb
Update task.md
ImVietnam Jun 12, 2023
e3d955c
Update solution.md
ImVietnam Jun 12, 2023
e594d50
Update task.md
ImVietnam Jun 12, 2023
7d8f5ff
Update solution.md
ImVietnam Jun 12, 2023
5833a1a
Update task.md
ImVietnam Jun 12, 2023
1da127f
Update article.md
ImVietnam Jun 12, 2023
7f3a61d
Merge branch 'javascript-tutorial:master' into patch-15
ImVietnam Aug 2, 2023
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
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
function camelize(str) {
return str
.split('-') // splits 'my-long-word' into array ['my', 'long', 'word']
.split('-') // tách 'my-long-word' thành array ['my', 'long', 'word']
.map(
// capitalizes first letters of all array items except the first one
// converts ['my', 'long', 'word'] into ['my', 'Long', 'Word']
// viết hoa các chữ cái đầu tiên của tất cả các phần tử array ngoại trừ phần tử đầu tiên
// chuyển ['my', 'long', 'word'] thành ['my', 'Long', 'Word']
(word, index) => index == 0 ? word : word[0].toUpperCase() + word.slice(1)
)
.join(''); // joins ['my', 'Long', 'Word'] into 'myLongWord'
.join(''); // nối ['my', 'Long', 'Word'] thành 'myLongWord'
}
10 changes: 5 additions & 5 deletions 1-js/05-data-types/05-array-methods/1-camelcase/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ importance: 5

---

# Translate border-left-width to borderLeftWidth
# Dịch border-left-width sang borderLeftWidth

Write the function `camelize(str)` that changes dash-separated words like "my-short-string" into camel-cased "myShortString".
Viết hàm `camelize(str)` để thay đổi các từ được phân tách bằng dấu gạch ngang như "my-short-string" thành "myShortString" có viết hoa.

That is: removes all dashes, each word after dash becomes uppercased.
Đó là: loại bỏ tất cả các dấu gạch ngang, mỗi từ sau dấu gạch ngang sẽ được viết hoa.

Examples:
Ví dụ:

```js
camelize("background-color") == 'backgroundColor';
camelize("list-style-image") == 'listStyleImage';
camelize("-webkit-transition") == 'WebkitTransition';
```

P.S. Hint: use `split` to split the string into an array, transform it and `join` back.
Tái bút: Gợi ý: sử dụng `split` để chia chuỗi thành một array, biến đổi nó và `join` trở lại.
8 changes: 4 additions & 4 deletions 1-js/05-data-types/05-array-methods/10-average-age/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ importance: 4

---

# Get average age
# Lấy tuổi trung bình

Write the function `getAverageAge(users)` that gets an array of objects with property `age` and returns the average age.
Viết hàm `getAverageAge(users)` để lấy một array các đối tượng có thuộc tính `age` và trả về tuổi trung bình.

The formula for the average is `(age1 + age2 + ... + ageN) / N`.
Công thức tính trung bình là `(age1 + age2 + ... + ageN) / N`.

For instance:
Ví dụ:

```js no-beautify
let john = { name: "John", age: 25 };
Expand Down
22 changes: 11 additions & 11 deletions 1-js/05-data-types/05-array-methods/11-array-unique/solution.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Let's walk the array items:
- For each item we'll check if the resulting array already has that item.
- If it is so, then ignore, otherwise add to results.
Hãy đi qua các item array:
- Đối với mỗi item, chúng ta sẽ kiểm tra xem kết quả array đã có item đó chưa.
- Nếu đúng thì bỏ qua, nếu không thì cộng vào kết quả.

```js run demo
function unique(arr) {
Expand All @@ -22,18 +22,18 @@ let strings = ["Hare", "Krishna", "Hare", "Krishna",
alert( unique(strings) ); // Hare, Krishna, :-O
```

The code works, but there's a potential performance problem in it.
Mã này hoạt động, nhưng có một vấn đề tiềm ẩn về hiệu suất trong đó.

The method `result.includes(str)` internally walks the array `result` and compares each element against `str` to find the match.
Phương thức `result.includes(str)` đi bên trong array `result` và so sánh từng phần tử với `str` để tìm kết quả khớp.

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.
Vì vậy, nếu có `100` phần tử trong `result` và không có phần tử nào khớp với `str`, thì nó sẽ duyệt toàn bộ `result` và thực hiện so sánh chính xác `100`. Và nếu `result` lớn, chẳng hạn như `10000`, thì sẽ có các phép so sánh `10000`.

That's not a problem by itself, because JavaScript engines are very fast, so walk `10000` array is a matter of microseconds.
Bản thân nó không phải là vấn đề, bởi vì các JavaScript engine rất nhanh, do đó, array `10000` walk chỉ là một phần triệu giây.

But we do such test for each element of `arr`, in the `for` loop.
Nhưng chúng ta thực hiện kiểm tra như vậy cho từng phần tử của `arr`, trong vòng lặp `for`.

So if `arr.length` is `10000` we'll have something like `10000*10000` = 100 millions of comparisons. That's a lot.
Vì vậy, nếu `arr.length` `10000` thì chúng ta sẽ có thứ gì đó như `10000*10000` = 100 triệu phép so sánh. Đó là rất nhiều.

So the solution is only good for small arrays.
Vì vậy, giải pháp chỉ tốt cho các array nhỏ.

Further in the chapter <info:map-set> we'll see how to optimize it.
Hơn nữa trong chương <info:map-set> chúng ta sẽ xem cách tối ưu hóa nó.
8 changes: 4 additions & 4 deletions 1-js/05-data-types/05-array-methods/11-array-unique/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ importance: 4

---

# Filter unique array members
# Lọc các thành viên array duy nhất

Let `arr` be an array.
Đặt `arr` là một array.

Create a function `unique(arr)` that should return an array with unique items of `arr`.
Tạo một hàm `unique(arr)` sẽ trả về một array với các phần tử duy nhất của `arr`.

For instance:
Ví dụ:

```js
function unique(arr) {
Expand Down
16 changes: 8 additions & 8 deletions 1-js/05-data-types/05-array-methods/12-reduce-object/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ importance: 4

---

# Create keyed object from array
# Tạo đối tượng có khóa từ array

Let's say we received an array of users in the form `{id:..., name:..., age... }`.
Giả sử chúng ta nhận được một array người dùng ở dạng `{id:..., name:..., age... }`.

Create a function `groupById(arr)` that creates an object from it, with `id` as the key, and array items as values.
Tạo một hàm `groupById(arr)` để tạo một đối tượng từ nó, với `id` là khóa và các mục array là giá trị.

For example:
Ví dụ:

```js
let users = [
Expand All @@ -20,7 +20,7 @@ let users = [
let usersById = groupById(users);

/*
// after the call we should have:
// sau cuộc gọi chúng ta nên có:

usersById = {
john: {id: 'john', name: "John Smith", age: 20},
Expand All @@ -30,8 +30,8 @@ usersById = {
*/
```

Such function is really handy when working with server data.
Hàm như vậy thực sự tiện dụng khi làm việc với dữ liệu máy chủ.

In this task we assume that `id` is unique. There may be no two array items with the same `id`.
Trong nhiệm vụ này, chúng ta giả định rằng `id` là duy nhất. Có thể không có hai mục array nào có cùng `id`.

Please use array `.reduce` method in the solution.
Hãy sử dụng phương thức array `.reduce` trong giải pháp.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

function filterRange(arr, a, b) {
// added brackets around the expression for better readability
// thêm dấu ngoặc xung quanh biểu thức để dễ đọc hơn
return arr.filter(item => (a <= item && item <= b));
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
```js run demo
function filterRange(arr, a, b) {
// added brackets around the expression for better readability
// thêm dấu ngoặc xung quanh biểu thức để dễ đọc hơn
return arr.filter(item => (a <= item && item <= b));
}

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

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

alert( filtered ); // 3,1 (matching values)
alert( filtered ); // 3,1 (giá trị trùng khớp)

alert( arr ); // 5,3,8,1 (not modified)
alert( arr ); // 5,3,8,1 (không bị sửa đổi)
```
13 changes: 6 additions & 7 deletions 1-js/05-data-types/05-array-methods/2-filter-range/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ importance: 4

---

# Filter range
# Phạm vi bộ lọc

Write a function `filterRange(arr, a, b)` that gets an array `arr`, looks for elements with values higher or equal to `a` and lower or equal to `b` and return a result as an array.
Viết một hàm `filterRange(arr, a, b)` nhận một array `arr`, tìm kiếm các phần tử có giá trị cao hơn hoặc bằng `a` và thấp hơn hoặc bằng `b` và trả về kết quả dưới dạng một array.

The function should not modify the array. It should return the new array.
Hàm không nên sửa đổi array. Nó sẽ trả về array mới.

For instance:
Ví dụ:

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

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

alert( filtered ); // 3,1 (matching values)
alert( filtered ); // 3,1 (giá trị trùng khớp)

alert( arr ); // 5,3,8,1 (not modified)
alert( arr ); // 5,3,8,1 (không bị sửa đổi)
```

Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ function filterRangeInPlace(arr, a, b) {
for (let i = 0; i < arr.length; i++) {
let val = arr[i];

// remove if outside of the interval
// loại bỏ nếu ở bên ngoài khoảng thời gian
if (val < a || val > b) {
arr.splice(i, 1);
i--;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function filterRangeInPlace(arr, a, b) {
for (let i = 0; i < arr.length; i++) {
let val = arr[i];

// remove if outside of the interval
// loại bỏ nếu ở bên ngoài khoảng thời gian
if (val < a || val > b) {
arr.splice(i, 1);
i--;
Expand All @@ -15,7 +15,7 @@ function filterRangeInPlace(arr, a, b) {

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

filterRangeInPlace(arr, 1, 4); // removed the numbers except from 1 to 4
filterRangeInPlace(arr, 1, 4); // loại bỏ các số trừ từ 1 đến 4

alert( arr ); // [3, 1]
```
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ importance: 4

---

# Filter range "in place"
# Phạm vi lọc "tại chỗ"

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`.
Viết một hàm `filterRangeInPlace(arr, a, b)` nhận một array `arr` và loại bỏ khỏi array đó tất cả các giá trị ngoại trừ các giá trị nằm trong khoảng từ `a` đến `b`. Phép thử là: `a ≤ arr[i] ≤ b`.

The function should only modify the array. It should not return anything.
Hàm chỉ nên sửa đổi array. Nó không nên trả lại bất cứ điều gì.

For instance:
Ví dụ:
```js
let arr = [5, 3, 8, 1];

filterRangeInPlace(arr, 1, 4); // removed the numbers except from 1 to 4
filterRangeInPlace(arr, 1, 4); // loại bỏ các số trừ từ 1 đến 4

alert( arr ); // [3, 1]
```
4 changes: 2 additions & 2 deletions 1-js/05-data-types/05-array-methods/4-sort-back/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ importance: 4

---

# Sort in decreasing order
# Sắp xếp theo thứ tự giảm dần

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

// ... your code to sort it in decreasing order
// ... mã của bạn để sắp xếp nó theo thứ tự giảm dần

alert( arr ); // 8, 5, 2, 1, -10
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
We can use `slice()` to make a copy and run the sort on it:
Chúng ta có thể sử dụng `slice()` để tạo một bản sao và chạy sắp xếp trên đó:

```js run
function copySorted(arr) {
Expand Down
8 changes: 4 additions & 4 deletions 1-js/05-data-types/05-array-methods/5-copy-sort-array/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ importance: 5

---

# Copy and sort array
# Sao chép và sắp xếp array

We have an array of strings `arr`. We'd like to have a sorted copy of it, but keep `arr` unmodified.
Chúng ta có một array với các chuỗi `arr`. Chúng ta muốn có một bản sao được sắp xếp của nó, nhưng giữ nguyên `arr`.

Create a function `copySorted(arr)` that returns such a copy.
Tạo một hàm `copySorted(arr)` để trả về một bản sao như vậy.

```js
let arr = ["HTML", "JavaScript", "CSS"];

let sorted = copySorted(arr);

alert( sorted ); // CSS, HTML, JavaScript
alert( arr ); // HTML, JavaScript, CSS (no changes)
alert( arr ); // HTML, JavaScript, CSS (không thay đổi)
```
8 changes: 4 additions & 4 deletions 1-js/05-data-types/05-array-methods/6-array-get-names/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ importance: 5

---

# Map to names
# Map đến names

You have an array of `user` objects, each one has `user.name`. Write the code that converts it into an array of names.
Bạn có một array các đối tượng `user`, mỗi đối tượng có `user.name`. Viết mã chuyển đổi nó thành một array names.

For instance:
Ví dụ:

```js no-beautify
let john = { name: "John", age: 25 };
Expand All @@ -15,7 +15,7 @@ let mary = { name: "Mary", age: 28 };

let users = [ john, pete, mary ];

let names = /* ... your code */
let names = /* ... mã của bạn */

alert( names ); // John, Pete, Mary
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

- Please note how methods are stored. They are simply added to `this.methods` property.
- All tests and numeric conversions are done in the `calculate` method. In future it may be extended to support more complex expressions.
- Hãy lưu ý cách các phương thức được lưu trữ. Chúng chỉ được thêm vào thuộc tính `this.methods`.
- Tất cả các kiểm tra và chuyển đổi số được thực hiện theo phương thức `calculate`. Trong tương lai, nó có thể được mở rộng để hỗ trợ các biểu thức phức tạp hơn.
26 changes: 13 additions & 13 deletions 1-js/05-data-types/05-array-methods/6-calculator-extendable/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@ importance: 5

---

# Create an extendable calculator
# Tạo một máy tính có thể mở rộng

Create a constructor function `Calculator` that creates "extendable" calculator objects.
Tạo một constructor `Calculator` để tạo các đối tượng máy tính "có thể mở rộng".

The task consists of two parts.
Nhiệm vụ bao gồm hai phần.

1. First, implement the method `calculate(str)` that takes a string like `"1 + 2"` in the format "NUMBER operator NUMBER" (space-delimited) and returns the result. Should understand plus `+` and minus `-`.
1. Đầu tiên, triển khai phương thức `calculate(str)` nhận một chuỗi như `"1 + 2"` ở định dạng "SỐ toán tử SỐ" (phân cách bằng dấu cách) và trả về kết quả. Nên hiểu cộng `+` và trừ `-`.

Usage example:
Ví dụ sử dụng:

```js
let calc = new Calculator;

alert( calc.calculate("3 + 7") ); // 10
```
2. Then add the method `addMethod(name, func)` that teaches the calculator a new operation. It takes the operator `name` and the two-argument function `func(a,b)` that implements it.
2. Sau đó thêm phương thức `addMethod(name, func)` dạy cho máy tính một thao tác mới. Nó sử dụng toán tử `name` và hàm hai đối số `func(a,b)` để thực hiện nó.

For instance, let's add the multiplication `*`, division `/` and power `**`:
Chẳng hạn, hãy thêm phép nhân `*`, phép chia `/` và lũy thừa `**`:

```js
let powerCalc = new Calculator;
powerCalc.addMethod("*", (a, b) => a * b);
powerCalc.addMethod("/", (a, b) => a / b);
powerCalc.addMethod("**", (a, b) => a ** b);
powerCalc.addMethod("nhân", (a, b) => a * b);
powerCalc.addMethod("chia", (a, b) => a / b);
powerCalc.addMethod("luỹ thừa", (a, b) => a ** b);

let result = powerCalc.calculate("2 ** 3");
alert( result ); // 8
```

- No parentheses or complex expressions in this task.
- The numbers and the operator are delimited with exactly one space.
- There may be error handling if you'd like to add it.
- Không có dấu ngoặc đơn hay biểu thức phức tạp trong nhiệm vụ này.
- Các số và toán tử được phân cách bằng đúng một dấu cách.
- Có thể xử lý lỗi nếu bạn muốn thêm nó.
Loading