Tell us what’s happening:
How may I refactor in here?
I tried and I changed the global variable when I do not want to do so.
Your code so far
// The global variable
var 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) {
var book_index = bookList.indexOf(bookName);
if (book_index >= 0) {
bookList.splice(book_index, 1);
return bookList;
// Change 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);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36.
Challenge: Refactor Global Variables Out of Functions
I tried it, but I erased beacause it was wrong: only a declared a new variable inside add. And I tried to console.out() outside the function and it did not work it. I also realized that I was changing my global variable.
If you do not want to change the global var, you to return a new array inside add and remove. So somehow make a copy, update it and return it might be close to d solution
I still change the global variable. How may I stop that?
// The global variable
var 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 ( bookList , bookName ) {
bookList.push( bookName );
return bookList;
// Change code above this line
}
// Change code below this line
function remove ( bookName ) {
var book_index = bookList.indexOf( bookName );
if ( book_index >= 0 ) {
bookList.splice( book_index, 1 );
return bookList;
// Change code above this line
}
}
var newBookList = add( bookList,
'A Brief History of Time');
console.log( bookList );
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' );
Rewrite the code so the global array bookList is not changed inside either function. The add function should add the given bookName to the end of the array passed to it and return a new array (list). The remove function should remove the given bookName from the array passed to it.
So you need to make a new array, modify this new array, and return the modified array.
I am tryin to get get the second challenge accomplished now.
// The global variable
var bookList = ["The Hound of the Baskervilles",
"On The Electrodynamics of Moving Bodies",
"Philosophiæ Naturalis Principia Mathematica",
"Disquisitiones Arithmeticae"];
// Change code below this line
let newArr= [...bookList]
function add ( bookList , bookName ) {
newArr.push( bookName );
// Change code above this line
}
// Change code below this line
function remove ( bookName ) {
var book_index = bookList.indexOf( bookName );
if ( book_index >= 0 ) {
bookList.splice( book_index, 1 );
return bookList;
// Change code above this line
}
}
var newBookList = add( bookList,
'A Brief History of Time');
console.log( newArr );
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' );
// The global variable
var bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"];
function add (booklist,bookname){
let newArr= [...bookList] ;
newArr.push(bookname)
//return newArr.push(bookname)
//newArr.splice(,)
//console.log(newArr)
}
var newBookList = add(bookList, 'A Brief History of Time');
//console.log(newArr)
/*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');
*/
// The global variable
var bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"];
function add (booklist,bookname){
let newArr= [...bookList] ;
newArr.push(bookname)
//return newArr.push(bookname)
return newArr.slice((newArr.indexOf(bookname)),(newArr.indexOf(bookname)));
}
var newBookList = add(bookList, 'A Brief History of Time');
//console.log(newArr)
/*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');
*/
I think you are getting yourself swisted in circles. I would reset the code in the challenge.
You need to take the existing functions and modify them to do the exact same thing but with a local copy of the bookList instead of using the global variable.
// The global variable
var 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) {
newArr.push(bookName);
return newArr;
// Change code above this line
}
// Change code below this line
function remove (bookName) {
var book_index = newArr.indexOf(bookName);
if (book_index >= 0) {
newArr.splice(book_index, 1);
return newArr;
// Change code above this line
}
}
let newArr=[...bookList];
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);
// The global variable
var 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 (bookList, bookName) {
let newArr=[...bookList]
newArr.push(bookName);
return newArr;
// Change code above this line
}
// Change code below this line
function remove (bookList,bookName) {
let arr= [...bookList];
var book_index = arr.indexOf(bookName);
if (book_index >= 0) {
arr.splice(book_index, 1);
return arr;
// Change 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);