Files

134 lines
4.6 KiB
Markdown
Raw Permalink Normal View History

---
id: 587d7b8f367417b2b2512b60
title: Refactoriza variables globales por fuera de funciones
challengeType: 1
forumTopicId: 301235
dashedName: refactor-global-variables-out-of-functions
---
# --description--
Hasta ahora, hemos visto dos principios distintos para la programación funcional:
1) No alteres una variable u objeto: crea nuevas variables y objetos y devuélvelos, si es necesario, desde una función. Pista: usar algo como `const newArr = arrVar`, donde `arrVar` es un arreglo, simplemente creará una referencia a la variable existente y no una copia. Así que cambiar un valor en `newArr` cambiaría el valor en `arrVar`.
2) Declara parámetros de función: cualquier cálculo dentro de una función depende sólo de los argumentos pasados a la función y no en ningún objeto o variable global.
Añadir uno a un número no es muy emocionante, pero podemos aplicar estos principios cuando trabajamos con arreglos u objetos más complejos.
# --instructions--
Reescribe el código para que el arreglo global `bookList` no sea cambiado dentro de ninguna de las funciones. La función `add` debe agregar el `bookName` dado al final del arreglo pasado a esta y devolver un nuevo arreglo (lista). La función `remove` debe eliminar el `bookName` dado del arreglo pasado a esta.
**Nota:** ambas funciones deben devolver un arreglo y cualquier nuevo parámetro debe ser añadido antes del parámetro `bookName`.
# --hints--
`bookList` no debe cambiar y aún ser igual a `["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")` debe devolver `["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")` debe devolver `["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");` debe ser igual a `["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;
}
```