Files

134 lines
4.4 KiB
Markdown
Raw Permalink Normal View History

---
id: 587d7b8f367417b2b2512b60
title: 在函數中重構全局變量
challengeType: 1
forumTopicId: 301235
dashedName: refactor-global-variables-out-of-functions
---
# --description--
目前爲止,我們已經看到了函數式編程的兩個原則:
1) 不要更改變量或對象 - 創建新變量和對象,並在需要時從函數返回它們。 提示:使用類似 `const newArr = arrVar` 的東西,其中 `arrVar` 是一個數組,只會創建對現有變量的引用,而不是副本。 所以更改 `newArr` 中的值會同時更改 `arrVar` 中的值。
2) 聲明函數參數 - 函數內的任何計算僅取決於參數,而不取決於任何全局對象或變量。
給數字增加 1 不夠刺激,我們可以在處理數組或更復雜的對象時應用這些原則。
# --instructions--
重構代碼,使全局數組 `bookList` 在函數內部不會被改變。 `add` 函數可以將指定的 `bookName` 增加到數組末尾並返回一個新的數組(列表)。 `remove` 函數可以從數組中移除指定 `bookName`
**注意:** 兩個函數都應該返回一個數組,任何新參數都應該在 `bookName` 參數之前添加。
# --hints--
`bookList` 應等於 `["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"]`.
```js
add(bookList, "Test");
remove(bookList, "The Hound of the Baskervilles");
assert(
JSON.stringify(bookList) ===
JSON.stringify([
'The Hound of the Baskervilles',
'On The Electrodynamics of Moving Bodies',
'Philosophiæ Naturalis Principia Mathematica',
'Disquisitiones Arithmeticae'
])
);
```
`add(bookList, "A Brief History of Time")` 應該返回 `["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae", "A Brief History of Time"]`
```js
assert(
JSON.stringify(add(bookList, "A Brief History of Time")) ===
JSON.stringify([
'The Hound of the Baskervilles',
'On The Electrodynamics of Moving Bodies',
'Philosophiæ Naturalis Principia Mathematica',
'Disquisitiones Arithmeticae',
'A Brief History of Time'
])
);
```
`remove(bookList, "On The Electrodynamics of Moving Bodies")` 應該返回 `["The Hound of the Baskervilles", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"]`
```js
assert(
JSON.stringify(remove(bookList, 'On The Electrodynamics of Moving Bodies')) ===
JSON.stringify([
'The Hound of the Baskervilles',
'Philosophiæ Naturalis Principia Mathematica',
'Disquisitiones Arithmeticae'
])
);
```
`remove(add(bookList, "A Brief History of Time"), "On The Electrodynamics of Moving Bodies");` 應該返回 `["The Hound of the Baskervilles", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae", "A Brief History of Time"]`
```js
assert(
JSON.stringify(remove(add(bookList, 'A Brief History of Time'), 'On The Electrodynamics of Moving Bodies')) ===
JSON.stringify([
'The Hound of the Baskervilles',
'Philosophiæ Naturalis Principia Mathematica',
'Disquisitiones Arithmeticae',
'A Brief History of Time'
])
);
```
# --seed--
## --seed-contents--
```js
// The global variable
const bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"];
// Change code below this line
function add(bookName) {
bookList.push(bookName);
return bookList;
// Change code above this line
}
// Change code below this line
function remove(bookName) {
const book_index = bookList.indexOf(bookName);
if (book_index >= 0) {
bookList.splice(book_index, 1);
return bookList;
// Change code above this line
}
}
```
# --solutions--
```js
// The global variable
const bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"];
function add(bookList, bookName) {
return [...bookList, bookName];
}
function remove(bookList, bookName) {
const bookListCopy = [...bookList];
const bookNameIndex = bookList.indexOf(bookName);
if (bookNameIndex >= 0) {
bookListCopy.splice(bookNameIndex, 1);
}
return bookListCopy;
}
```