--- id: 587d7b8f367417b2b2512b60 title: Refactor Global Variables Out of Functions challengeType: 1 forumTopicId: 301235 localeTitle: Глобальные переменные Refactor вне функций --- ## Description
До сих пор мы видели два разных принципа функционального программирования: 1) Не изменяйте переменную или объект - создавайте новые переменные и объекты и возвращайте их, если необходимо, из функции. 2) Объявлять аргументы функции - любое вычисление внутри функции зависит только от аргументов, а не от любого глобального объекта или переменной. Добавление одного к числу не очень интересно, но мы можем применять эти принципы при работе с массивами или более сложными объектами.
## Instructions
Refactor (переписать) код, поэтому глобальный массив bookList не изменяется внутри любой функции. Функция add должна добавить данное имя bookName в конец массива. Функция remove должна удалить данную bookName из массива. Обе функции должны возвращать массив, и любые новые параметры должны быть добавлены до имени bookName .
## Tests
```yml tests: - text: bookList should not change and still equal ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"]. testString: assert(JSON.stringify(bookList) === JSON.stringify(["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"])); - text: newBookList should equal ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae", "A Brief History of Time"]. testString: assert(JSON.stringify(newBookList) === JSON.stringify(['The Hound of the Baskervilles', 'On The Electrodynamics of Moving Bodies', 'Philosophiæ Naturalis Principia Mathematica', 'Disquisitiones Arithmeticae', 'A Brief History of Time'])); - text: newerBookList should equal ["The Hound of the Baskervilles", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"]. testString: assert(JSON.stringify(newerBookList) === JSON.stringify(['The Hound of the Baskervilles', 'Philosophiæ Naturalis Principia Mathematica', 'Disquisitiones Arithmeticae'])); - text: newestBookList should equal ["The Hound of the Baskervilles", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae", "A Brief History of Time"]. testString: assert(JSON.stringify(newestBookList) === JSON.stringify(['The Hound of the Baskervilles', 'Philosophiæ Naturalis Principia Mathematica', 'Disquisitiones Arithmeticae', 'A Brief History of Time'])); ```
## Challenge Seed
```js // the global variable var bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"]; /* This function should add a book to the list and return the list */ // New parameters should come before bookName // Add your code below this line function add (bookName) { bookList.push(bookName); return bookList; // Add your code above this line } /* This function should remove a book from the list and return the list */ // New parameters should come before the bookName one // Add your code below this line function remove (bookName) { var book_index = bookList.indexOf(bookName); if (book_index >= 0) { bookList.splice(book_index, 1); return bookList; // Add your code above this line } } var newBookList = add(bookList, 'A Brief History of Time'); var newerBookList = remove(bookList, 'On The Electrodynamics of Moving Bodies'); var newestBookList = remove(add(bookList, 'A Brief History of Time'), 'On The Electrodynamics of Moving Bodies'); console.log(bookList); ```
## Solution
```js // the global variable var bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"]; /* This function should add a book to the list and return the list */ // New parameters should come before the bookName one // Add your code below this line function add (bookList, bookName) { return [...bookList, bookName]; // Add your code above this line } /* This function should remove a book from the list and return the list */ // New parameters should come before the bookName one // Add your code below this line function remove (bookList, bookName) { const bookListCopy = [...bookList]; const bookNameIndex = bookList.indexOf(bookName); if (bookNameIndex >= 0) { bookListCopy.splice(bookNameIndex, 1); } return bookListCopy; // Add your code above this line } var newBookList = add(bookList, 'A Brief History of Time'); var newerBookList = remove(bookList, 'On The Electrodynamics of Moving Bodies'); var newestBookList = remove(add(bookList, 'A Brief History of Time'), 'On The Electrodynamics of Moving Bodies'); ```