5.7 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d7b8f367417b2b2512b60 | Зміна глобальних змінних поза функціями | 1 | 301235 | refactor-global-variables-out-of-functions |
--description--
На сьогодні ми спостерігаємо два окремих принципи функціонального програмування:
-
Не змінюйте змінну чи об'єкт - створюйте нові змінні і об'єкти та повертайте їх через функцію, якщо це необхідно. Підказка: використання чогось на зразок
const newArr = arrVar
(деarrVar
є масивом) просто створить посилання на оригінальну змінну, а не копію. Таким чином, зміна значення вnewArr
змінить значення вarrVar
. -
Оголошення параметрів функції - будь-яке обчислення всередині функції залежить лише від аргументів, переданих функції, а не від будь-якого глобального об'єкту чи змінної.
Додавання такого числа не є дуже захопливим, але ми можемо застосувати ці принципи при роботі з масивами або більш складними об'єктами.
--instructions--
Перепишіть код так, щоб глобальний масив bookList
не змінювався всередині кожної функції. Функція add
повинна додати цей код bookName
в кінець масиву та повернути новий масив ( список). Функція remove
повинна видалити вказану bookName
з масиву, переданого до неї.
Note: Обидві функції повинні повертати масив, і будь-які нові параметри повинні бути додані перед параметром bookName
.
--hints--
bookList
не повинен змінюватися і, як і раніше, дорівнювати ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"]
.
assert(
JSON.stringify(bookList) ===
JSON.stringify([
'The Hound of the Baskervilles',
'On The Electrodynamics of Moving Bodies',
'Philosophiæ Naturalis Principia Mathematica',
'Disquisitiones Arithmeticae'
])
);
newBookList
повинен дорівнювати ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae", "A Brief History of Time"]
.
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'
])
);
newerBookList
повинен дорівнювати ["The Hound of the Baskervilles", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"]
.
assert(
JSON.stringify(newerBookList) ===
JSON.stringify([
'The Hound of the Baskervilles',
'Philosophiæ Naturalis Principia Mathematica',
'Disquisitiones Arithmeticae'
])
);
newestBookList
повинен дорівнювати ["The Hound of the Baskervilles", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae", "A Brief History of Time"]
.
assert(
JSON.stringify(newestBookList) ===
JSON.stringify([
'The Hound of the Baskervilles',
'Philosophiæ Naturalis Principia Mathematica',
'Disquisitiones Arithmeticae',
'A Brief History of Time'
])
);
--seed--
--seed-contents--
// 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
}
}
const newBookList = add(bookList, 'A Brief History of Time');
const newerBookList = remove(bookList, 'On The Electrodynamics of Moving Bodies');
const newestBookList = remove(add(bookList, 'A Brief History of Time'), 'On The Electrodynamics of Moving Bodies');
console.log(bookList);
--solutions--
// 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;
}
const newBookList = add(bookList, 'A Brief History of Time');
const newerBookList = remove(bookList, 'On The Electrodynamics of Moving Bodies');
const newestBookList = remove(add(bookList, 'A Brief History of Time'), 'On The Electrodynamics of Moving Bodies');