feat: refactor the refactor challenge (#44770)

This commit is contained in:
Nicholas Carrigan (he/him)
2022-01-17 00:57:15 -08:00
committed by GitHub
parent fd9c68ceef
commit 8954449625

View File

@ -27,6 +27,7 @@ Rewrite the code so the global array `bookList` is not changed inside either fun
`bookList` should not change and still equal `["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"]`. `bookList` should not change and still equal `["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"]`.
```js ```js
add(bookList, "Test");
assert( assert(
JSON.stringify(bookList) === JSON.stringify(bookList) ===
JSON.stringify([ JSON.stringify([
@ -38,11 +39,11 @@ assert(
); );
``` ```
`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"]`. `add(bookList, "A Brief History of Time")` should return `["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae", "A Brief History of Time"]`.
```js ```js
assert( assert(
JSON.stringify(newBookList) === JSON.stringify(add(bookList, "A Brief History of Time")) ===
JSON.stringify([ JSON.stringify([
'The Hound of the Baskervilles', 'The Hound of the Baskervilles',
'On The Electrodynamics of Moving Bodies', 'On The Electrodynamics of Moving Bodies',
@ -53,11 +54,11 @@ assert(
); );
``` ```
`newerBookList` should equal `["The Hound of the Baskervilles", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"]`. `remove(bookList, "On The Electrodynamics of Moving Bodies")` should return `["The Hound of the Baskervilles", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"]`.
```js ```js
assert( assert(
JSON.stringify(newerBookList) === JSON.stringify(remove(bookList, 'On The Electrodynamics of Moving Bodies')) ===
JSON.stringify([ JSON.stringify([
'The Hound of the Baskervilles', 'The Hound of the Baskervilles',
'Philosophiæ Naturalis Principia Mathematica', 'Philosophiæ Naturalis Principia Mathematica',
@ -66,11 +67,11 @@ assert(
); );
``` ```
`newestBookList` should equal `["The Hound of the Baskervilles", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae", "A Brief History of Time"]`. `remove(add(bookList, "A Brief History of Time"), "On The Electrodynamics of Moving Bodies");` should equal `["The Hound of the Baskervilles", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae", "A Brief History of Time"]`.
```js ```js
assert( assert(
JSON.stringify(newestBookList) === JSON.stringify(remove(add(bookList, 'A Brief History of Time'), 'On The Electrodynamics of Moving Bodies')) ===
JSON.stringify([ JSON.stringify([
'The Hound of the Baskervilles', 'The Hound of the Baskervilles',
'Philosophiæ Naturalis Principia Mathematica', 'Philosophiæ Naturalis Principia Mathematica',
@ -108,12 +109,6 @@ function remove(bookName) {
// Change code above this line // 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-- # --solutions--
@ -134,8 +129,4 @@ function remove(bookList, bookName) {
} }
return bookListCopy; 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');
``` ```