Skip to content
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

Map and Set #138

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 7 additions & 7 deletions 1-js/05-data-types/07-map-set/01-array-unique-map/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ importance: 5

---

# Filter unique array members
# 過濾掉重複的陣列成員

Let `arr` be an array.
`arr` 為一個陣列。

Create a function `unique(arr)` that should return an array with unique items of `arr`.
建立一個傳回沒有重複項目的函式 `unique(arr)`

For instance:
例如:

```js
function unique(arr) {
/* your code */
/* 你的程式碼 */
}

let values = ["Hare", "Krishna", "Hare", "Krishna",
Expand All @@ -22,6 +22,6 @@ let values = ["Hare", "Krishna", "Hare", "Krishna",
alert( unique(values) ); // Hare, Krishna, :-O
```

P.S. Here strings are used, but can be values of any type.
附註:這裡雖然是用了字串,但也要可以是任何型別。

P.P.S. Use `Set` to store unique values.
追加附註:使用 `Set` 去儲放不重複的值。
24 changes: 12 additions & 12 deletions 1-js/05-data-types/07-map-set/02-filter-anagrams/solution.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
To find all anagrams, let's split every word to letters and sort them. When letter-sorted, all anagrams are same.
找到所有易位構詞(anagrams)讓我們分解每個單字為字母並為其排序。當排序好字母時,所有易位構詞是相同的。

For instance:
例如:

```
nap, pan -> anp
Expand All @@ -9,14 +9,14 @@ cheaters, hectares, teachers -> aceehrst
...
```

We'll use the letter-sorted variants as map keys to store only one value per each key:
我們將使用字母排序後的變動值作為對應鍵值,每個鍵值僅存儲一個值:

```js run
function aclean(arr) {
let map = new Map();

for (let word of arr) {
// split the word by letters, sort them and join back
// 分解單字成字母,排序他們之後在合併起來
*!*
let sorted = word.toLowerCase().split('').sort().join(''); // (*)
*/!*
Expand All @@ -31,9 +31,9 @@ let arr = ["nap", "teachers", "cheaters", "PAN", "ear", "era", "hectares"];
alert( aclean(arr) );
```

Letter-sorting is done by the chain of calls in the line `(*)`.
字母排序是藉由呼叫串鏈,那行後面註解有個 `(*)`

For convenience let's split it into multiple lines:
為了方便了解,我們分解成多行:

```js
let sorted = word // PAN
Expand All @@ -43,21 +43,21 @@ let sorted = word // PAN
.join(''); // anp
```

Two different words `'PAN'` and `'nap'` receive the same letter-sorted form `'anp'`.
收到 `'PAN'` `'nap'` 兩個不同單字為相同字母排列形式 `'anp'`

The next line put the word into the map:
在下一行把這些單字放入 map

```js
map.set(sorted, word);
```

If we ever meet a word the same letter-sorted form again, then it would overwrite the previous value with the same key in the map. So we'll always have at maximum one word per letter-form.
如果我們再次遇到相同字母排列形式的單字,那麼該單字將會在 map 的鍵值上覆蓋原先的值。因此,每個字母形式最多只能有一個單字。

At the end `Array.from(map.values())` takes an iterable over map values (we don't need keys in the result) and returns an array of them.
最後 `Array.from(map.values())` map 中的值進行迭代(我們不需要鍵值)並傳回它們的陣列。

Here we could also use a plain object instead of the `Map`, because keys are strings.
在這裡我們也可以使用一個普通物件而不是 `Map`,因為鍵值是字串。

That's how the solution can look:
這就是解決方案供參:

```js run demo
function aclean(arr) {
Expand Down
12 changes: 6 additions & 6 deletions 1-js/05-data-types/07-map-set/02-filter-anagrams/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ importance: 4

---

# Filter anagrams
# 過濾易位構詞

[Anagrams](https://en.wikipedia.org/wiki/Anagram) are words that have the same number of same letters, but in different order.
[易位構詞](https://zh.wikipedia.org/wiki/易位構詞遊戲) 是具有相同數量字母但順序不同的單字。

For instance:
例如:

```
nap - pan
ear - are - era
cheaters - hectares - teachers
```

Write a function `aclean(arr)` that returns an array cleaned from anagrams.
撰寫一個函式 `aclean(arr)`,傳回不重複的易位構詞陣列。

For instance:
例如:

```js
let arr = ["nap", "teachers", "cheaters", "PAN", "ear", "era", "hectares"];

alert( aclean(arr) ); // "nap,teachers,ear" or "PAN,cheaters,era"
```

From every anagram group should remain only one word, no matter which one.
在每個易位構詞群組中,無論哪個單字,都應只保留一個單字。

4 changes: 2 additions & 2 deletions 1-js/05-data-types/07-map-set/03-iterable-keys/solution.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

That's because `map.keys()` returns an iterable, but not an array.
那是因為 `map.keys()` 傳回的是可迭代的物件,但不是一個陣列。

We can convert it into an array using `Array.from`:
我們可以轉換它變成陣列使用 `Array.from`


```js run
Expand Down
8 changes: 4 additions & 4 deletions 1-js/05-data-types/07-map-set/03-iterable-keys/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ importance: 5

---

# Iterable keys
# 可迭代鍵值

We'd like to get an array of `map.keys()` in a variable and then do apply array-specific methods to it, e.g. `.push`.
我們想在變數中獲得一個執行 `map.keys()` 後的陣列,然後使用陣列的方法,像是 `.push`

But that doesn't work:
但那是不會動的:

```js run
let map = new Map();
Expand All @@ -21,4 +21,4 @@ keys.push("more");
*/!*
```

Why? How can we fix the code to make `keys.push` work?
為什麼?我們該如何修改程式碼使 `keys.push` 能動?
Loading