Skip to content
14 changes: 7 additions & 7 deletions 1-js/05-data-types/03-string/1-ucfirst/solution.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
We can't "replace" the first character, because strings in JavaScript are immutable.
Chúng ta không thể "thay thế" ký tự đầu tiên, bởi vì các chuỗi trong JavaScript là bất biến.

But we can make a new string based on the existing one, with the uppercased first character:
Nhưng chúng ta có thể tạo một chuỗi mới dựa trên chuỗi hiện có, với ký tự đầu tiên được viết hoa:

```js
let newStr = str[0].toUpperCase() + str.slice(1);
```

There's a small problem though. If `str` is empty, then `str[0]` is `undefined`, and as `undefined` doesn't have the `toUpperCase()` method, we'll get an error.
Có một vấn đề nhỏ mặc dù. Nếu `str` trống, thì `str[0]` `undefined` và vì `undefined` không có phương thức `toUpperCase()` nên chúng ta sẽ gặp lỗi.

There are two variants here:
Có hai biến thể ở đây:

1. Use `str.charAt(0)`, as it always returns a string (maybe empty).
2. Add a test for an empty string.
1. Sử dụng `str.charAt(0)`, vì nó luôn trả về một chuỗi (có thể trống).
2. Thêm kiểm tra cho chuỗi trống.

Here's the 2nd variant:
Đây là biến thể thứ 2:

```js run demo
function ucFirst(str) {
Expand Down
4 changes: 2 additions & 2 deletions 1-js/05-data-types/03-string/1-ucfirst/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# Uppercase the first character
# Chữ hoa ký tự đầu tiên

Write a function `ucFirst(str)` that returns the string `str` with the uppercased first character, for instance:
Ví dụ, viết hàm `ucFirst(str)` trả về chuỗi `str` với ký tự đầu tiên được viết hoa:

```js
ucFirst("john") == "John";
Expand Down
2 changes: 1 addition & 1 deletion 1-js/05-data-types/03-string/2-check-spam/solution.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
To make the search case-insensitive, let's bring the string to lower case and then search:
Để làm cho tìm kiếm không phân biệt chữ hoa chữ thường, hãy chuyển chuỗi thành chữ thường và sau đó tìm kiếm:

```js run demo
function checkSpam(str) {
Expand Down
6 changes: 3 additions & 3 deletions 1-js/05-data-types/03-string/2-check-spam/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ importance: 5

---

# Check for spam
# Kiểm tra spam

Write a function `checkSpam(str)` that returns `true` if `str` contains 'viagra' or 'XXX', otherwise `false`.
Viết hàm `checkSpam(str)` trả về `true` nếu `str` chứa 'viagra' hoặc 'XXX', nếu không thì `false`.

The function must be case-insensitive:
Hàm phải phân biệt chữ hoa chữ thường:

```js
checkSpam('buy ViAgRA now') == true
Expand Down
4 changes: 2 additions & 2 deletions 1-js/05-data-types/03-string/3-truncate/solution.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The maximal length must be `maxlength`, so we need to cut it a little shorter, to give space for the ellipsis.
Độ dài tối đa phải là `maxlength`, vì vậy chúng ta cần cắt nó ngắn hơn một chút để tạo khoảng trống cho dấu chấm lửng.

Note that there is actually a single Unicode character for an ellipsis. That's not three dots.
Lưu ý rằng thực tế có một ký tự Unicode duy nhất cho dấu chấm lửng. Đó không phải là ba chấm.

```js run demo
function truncate(str, maxlength) {
Expand Down
8 changes: 4 additions & 4 deletions 1-js/05-data-types/03-string/3-truncate/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ importance: 5

---

# Truncate the text
# Cắt bớt văn bản

Create a function `truncate(str, maxlength)` that checks the length of the `str` and, if it exceeds `maxlength` -- replaces the end of `str` with the ellipsis character `"…"`, to make its length equal to `maxlength`.
Tạo một hàm `truncate(str, maxlength)` để kiểm tra độ dài của `str` và, nếu nó vượt quá `maxlength` -- thay thế phần cuối của `str` bằng ký tự dấu chấm lửng `"…"`, để tạo chiều dài bằng `maxlength`.

The result of the function should be the truncated (if needed) string.
Kết quả của hàm phải là chuỗi bị cắt ngắn (nếu cần).

For instance:
Ví dụ:

```js
truncate("What I'd like to tell on this topic is:", 20) = "What I'd like to te…"
Expand Down
8 changes: 4 additions & 4 deletions 1-js/05-data-types/03-string/4-extract-currency/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ importance: 4

---

# Extract the money
# Trích xuất tiền

We have a cost in the form `"$120"`. That is: the dollar sign goes first, and then the number.
Chúng ta có chi phí ở dạng `"$120"`. Đó là: ký hiệu đô la đi trước, sau đó là số.

Create a function `extractCurrencyValue(str)` that would extract the numeric value from such string and return it.
Tạo một hàm `extractCurrencyValue(str)` sẽ trích xuất giá trị số từ chuỗi đó và trả về giá trị đó.

The example:
Ví dụ:

```js
alert( extractCurrencyValue('$120') === 120 ); // true
Expand Down
Loading