-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddingRemovingBooks.js
More file actions
43 lines (33 loc) · 1.61 KB
/
addingRemovingBooks.js
File metadata and controls
43 lines (33 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// *** Functional Programming: Refactor Global Variables Out of Functions ***
// 1) Don't alter a variable or object - create new variables and objects and return
// them if need be from a function.
// 2) Declare function arguments - any computation inside a function depends only on
// the arguments, and not on any global object or variable.
// ******************************************************************************* //
// 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) {
var bookListCopy = bookList.slice();
bookListCopy.push(bookName);
return bookListCopy;
// 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) {
var bookListCopy = bookList.slice();
var idx = bookListCopy.indexOf(bookName);
if (idx >= 0) {
bookListCopy.splice(idx, 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');
console.log(bookList);