Skip to content
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
1 change: 0 additions & 1 deletion 1-js/04-object-basics/01-object/2-hello-object/solution.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


```js
let user = {};
user.name = "John";
Expand Down
14 changes: 7 additions & 7 deletions 1-js/04-object-basics/01-object/2-hello-object/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ importance: 5

---

# Hello, object
# 哈囉,物件

Write the code, one line for each action:
寫段程式碼,每個動作各一行:

1. Create an empty object `user`.
2. Add the property `name` with the value `John`.
3. Add the property `surname` with the value `Smith`.
4. Change the value of the `name` to `Pete`.
5. Remove the property `name` from the object.
1. 建立一個空物件 `user`
2. 加入屬性 `name` 與值 `John`
3. 加入屬性 `surname` 與值 `Smith`
4. 改變 `name` 的值為 `Pete`
5. 從物件中移除屬性 `name`

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function isEmpty(obj) {
for (let key in obj) {
// if the loop has started, there is a property
// 若迴圈開始,代表有屬性存在
return false;
}
return true;
Expand Down
3 changes: 2 additions & 1 deletion 1-js/04-object-basics/01-object/3-is-empty/solution.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Just loop over the object and `return false` immediately if there's at least one property.
只要遍歷該物件並在至少有一個屬性時 `return false` 就好。

6 changes: 3 additions & 3 deletions 1-js/04-object-basics/01-object/3-is-empty/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ importance: 5

---

# Check for emptiness
# 檢查空物件

Write the function `isEmpty(obj)` which returns `true` if the object has no properties, `false` otherwise.
寫一個函式 `isEmpty(obj)`,若物件沒有屬性時回傳 `true`,否則回傳 `false`

Should work like that:
應該要像這樣運作:

```js
let schedule = {};
Expand Down
11 changes: 6 additions & 5 deletions 1-js/04-object-basics/01-object/4-const-object/solution.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
Sure, it works, no problem.
當然可運作,沒問題。

The `const` only protects the variable itself from changing.
`const` 只保護變數本身不被改變。

In other words, `user` stores a reference to the object. And it can't be changed. But the content of the object can.
換句話說,`user` 儲存物件的參考,且其無法被改變。但物件的內容卻可以改變。

```js run
const user = {
name: "John"
};

*!*
// works
// 可運作
user.name = "Pete";
*/!*

// error
// 錯誤
user = 123;
```

7 changes: 4 additions & 3 deletions 1-js/04-object-basics/01-object/4-const-object/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ importance: 5

---

# Constant objects?
# 常數物件?

Is it possible to change an object declared with `const`? What do you think?
有可能改變用 `const` 宣告的物件嗎?你怎麼想?

```js
const user = {
name: "John"
};

*!*
// does it work?
// 這能運作嗎?
user.name = "Pete";
*/!*
```

9 changes: 5 additions & 4 deletions 1-js/04-object-basics/01-object/5-sum-object/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# Sum object properties
# 加總物件屬性

We have an object storing salaries of our team:
我們有個物件存放著我們團隊的薪資:

```js
let salaries = {
Expand All @@ -14,6 +14,7 @@ let salaries = {
}
```

Write the code to sum all salaries and store in the variable `sum`. Should be `390` in the example above.
寫一段程式碼加總薪資並儲存在變數 `sum` 內。上面的例子應該要是 `390`。

若 `salaries` 為空,則結果必須為 `0`。

If `salaries` is empty, then the result must be `0`.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ let menu = {


function multiplyNumeric(obj) {
/* your code */

/* 你的程式碼 */

}

Expand Down
15 changes: 7 additions & 8 deletions 1-js/04-object-basics/01-object/8-multiply-numeric/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ importance: 3

---

# Multiply numeric properties by 2
# 數值屬性都乘以 2

Create a function `multiplyNumeric(obj)` that multiplies all numeric properties of `obj` by `2`.
建立一個函式 `multiplyNumeric(obj)` 來把 `obj` 的所有數值屬性都乘以 2。

For instance:
舉個例:

```js
// before the call
// 在呼叫前
let menu = {
width: 200,
height: 300,
Expand All @@ -18,16 +18,15 @@ let menu = {

multiplyNumeric(menu);

// after the call
// 在呼叫後
menu = {
width: 400,
height: 600,
title: "My menu"
};
```

Please note that `multiplyNumeric` does not need to return anything. It should modify the object in-place.

P.S. Use `typeof` to check for a number here.
請注意 `multiplyNumeric` 不需要回傳任何東西,它應該要原地(in place)修改物件。

註:使用 `typeof` 來辨別數值類型。

Loading