Refactor Global Variables Out of Functions (Stuck)

Hi all, I know I can look at the other posts with the same question, but most likely their code is different, and I want to know why my code is not working.

link to the question:

So everything passes except the output for newBookList.
Which is puzzling, because when I add a document.write(newBookList) underneath the line : var newBookList = add(tempBookList, ‘A Brief History of Time’);
it shows the correct output ?

Here’s my code:

var bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"];
let tempBookList = bookList.slice();

function add (list,bookName) {
  return list;
function remove (list,bookName) {
  var book_index = list.indexOf(bookName);
  if (book_index >= 0) {
    let begin=list.slice(0,book_index);
	let end = list.slice(book_index+1,list.length-1);
	let newlist= begin.concat(end);
    return newlist;
  } else {
	return list;

var newBookList = add(tempBookList, 'A Brief History of Time');
document.write("<br/><br/>"+ newBookList); //shows the correct output?
var newerBookList = remove(tempBookList, 'On The Electrodynamics of Moving Bodies');
const added = add(tempBookList,'A Brief History of Time');
var newestBookList = remove(added, 'On The Electrodynamics of Moving Bodies');

Thanks for any help ! :slight_smile:

next time when you ask for help please use the “Ask for help” button so that challenge link is included
without that we are missing the contest and not able to help you

please provide challenge link

Sorry, didn’t know I could do that. I added a link :slight_smile:

Rewrite the code so the global array bookList is not changed inside either function.

you are not respecting this
yes, you created a new global variable, but that’s not exactly how you should do it
you still changed that global variable so the outputs of the various functions are not what is expected

don’t use your temp variable, just make sure that your functions don’t change anything outside of them

and maybe you need to reset your code if your function calls got messed too much

I did solve it , and the solution was pretty simple in the end.