Skip to content

Functions #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 15, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -1 +1 @@
No difference.
Không có sự thay đổi nào.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ importance: 4

---

# Is "else" required?
# "else" có bắt buộc không?

The following function returns `true` if the parameter `age` is greater than `18`.
Hàm sau đây sẽ trả về `true` nếu tham số `age` là lớn hơn `18`.

Otherwise it asks for a confirmation and returns its result:
Nếu không thì nó sẽ hỏi một câu xác nhận và trả về kết quả của nó:

```js
function checkAge(age) {
Expand All @@ -15,13 +15,13 @@ function checkAge(age) {
*!*
} else {
// ...
return confirm('Did parents allow you?');
return confirm('Bố mẹ đã cho phép bạn chưa?');
}
*/!*
}
```

Will the function work differently if `else` is removed?
Hàm sẽ thực hiện khác đi nếu từ `else` bị lược bỏ hay không?

```js
function checkAge(age) {
Expand All @@ -30,9 +30,9 @@ function checkAge(age) {
}
*!*
// ...
return confirm('Did parents allow you?');
return confirm('Bố mẹ đã cho phép bạn chưa?');
*/!*
}
```

Is there any difference in the behavior of these two variants?
Có sự khác biệt nào trong hành vi của hai biến thể này không?
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
Using a question mark operator `'?'`:
Sử dụng toán tử dấu chấm hỏi `'?'`:

```js
function checkAge(age) {
return (age > 18) ? true : confirm('Did parents allow you?');
return (age > 18) ? true : confirm('Bố mẹ đã cho phép bạn chưa?');
}
```

Using OR `||` (the shortest variant):
Sử dụng OR `||` (biến thể ngắn nhất):

```js
function checkAge(age) {
return (age > 18) || confirm('Did parents allow you?');
return (age > 18) || confirm('Bố mẹ đã cho phép bạn chưa?');
}
```

Note that the parentheses around `age > 18` are not required here. They exist for better readability.
Lưu ý rằng dấu ngoặc đơn quanh `age > 18` là không bắt buộc. Chúng tồn tại để chúng ta dễ đọc hơn.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ importance: 4

---

# Rewrite the function using '?' or '||'
# Viết lại hàm sử dụng '?' hoặc '||'

The following function returns `true` if the parameter `age` is greater than `18`.

Expand Down
6 changes: 3 additions & 3 deletions 1-js/02-first-steps/15-function-basics/3-min/solution.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
A solution using `if`:
Một phương án sử dụng `if`:

```js
function min(a, b) {
Expand All @@ -10,12 +10,12 @@ function min(a, b) {
}
```

A solution with a question mark operator `'?'`:
Giải pháp với toán tử `'?'`:

```js
function min(a, b) {
return a < b ? a : b;
}
```

P.S. In the case of an equality `a == b` it does not matter what to return.
Tái bút: Trong trường hợp so sánh bằng nhau `a == b` thì không quan trọng giá trị trả về là gì.
6 changes: 3 additions & 3 deletions 1-js/02-first-steps/15-function-basics/3-min/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ importance: 1

---

# Function min(a, b)
# Hàm min(a, b)

Write a function `min(a,b)` which returns the least of two numbers `a` and `b`.
Viết một hàm `min(a,b)` trả về giá trị nhỏ nhất trong hai số `a` `b`.

For instance:
Ví dụ:

```js
min(2, 5) == 2
Expand Down
2 changes: 1 addition & 1 deletion 1-js/02-first-steps/15-function-basics/4-pow/solution.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let x = prompt("x?", '');
let n = prompt("n?", '');

if (n < 1) {
alert(`Power ${n} is not supported, use a positive integer`);
alert(`Số mũ ${n} không được hỗ trợ, hãy dùng một số nguyên dương`);
} else {
alert( pow(x, n) );
}
Expand Down
8 changes: 4 additions & 4 deletions 1-js/02-first-steps/15-function-basics/4-pow/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ importance: 4

---

# Function pow(x,n)
# Hàm pow(x,n)

Write a function `pow(x,n)` that returns `x` in power `n`. Or, in other words, multiplies `x` by itself `n` times and returns the result.
Viết một hàm `pow(x,n)` trả về biến `x` với số mũ `n`. Hoặc, nói theo cách khác, thì nhân `x` với chính nó `n` lần và trả về kết quả.

```js
pow(3, 2) = 3 * 3 = 9
pow(3, 3) = 3 * 3 * 3 = 27
pow(1, 100) = 1 * 1 * ...* 1 = 1
```

Create a web-page that prompts for `x` and `n`, and then shows the result of `pow(x,n)`.
Tạo 1 trang web gợi ý cho `x` `n`, và sau đó đưa ra kết quả của `pow(x,n)`.

[demo]

P.S. In this task the function should support only natural values of `n`: integers up from `1`.
Tái bút: Trong nhiệm vụ này, hàm chỉ hỗ trợ các giá trị tự nhiên của `n`: các số nguyên dương lớn hơn hoặc bằng `1`.
Loading