Refactor Global Variables Out of Functions - Reference arrays not working

I am trying to use clones of bookList so that I these new arrays can return added or deleted elements without modifying the original array. Can I please get a hint on what could have gone wrong? Thank you.

  **Your code so far**

// 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) {
let booklistadd = [...bookList]
booklistadd.push(bookName);
return booklistadd;

// Change code above this line
}

// Change code below this line
function remove (bookName) {
let bookRemoved = [...bookList]
const book_index = bookRemoved.indexOf(bookName);
if (book_index >= 0) {

  bookRemoved.splice(book_index, 1);
  return bookRemoved;

  // 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);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0

Challenge: Refactor Global Variables Out of Functions

Link to the challenge:

Something screwy is going on with those function signatures. Looking at the calls, the signature should be

function add(bookArr, bookName) {
  //...
}
1 Like

I tried that as well and I still don’t seem to get it:

// 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 (bookArr, bookName) {
  bookArr = [...bookList]
  bookArr.push(bookName);
  return bookList;
  
  // Change code above this line
}

// Change code below this line
function remove (bookArr, bookName) {
  bookArr = [...bookList]
  const book_index = bookArr.indexOf(bookName);
  if (book_index >= 0) {

    bookArr.splice(book_index, 1);
    return bookArr;

    // 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);

But you can’t reference the global variable inside of your function. That’s bad news.

Thank you for taking a look into this! :slightly_smiling_face:

I am supposed to change only the remove and add function so I do not know how to reference to the global array with this limitation…,

That’s the whole point. You should never reference the global array. You need to only use variables passed into the functions as arguments.

yes and I am still not getting it, do you know any resource I could reference to in order to understand this better ? If you do, I would really appreciate it! Thank you…

These are the correct function signatures.

Inside of these functions, you are only allowed to use the variables bookArr and bookName.

So lets look at the function calls to see what these are:

The bookList that you want to modify is passed in as an argument, so you shouldn’t reference a specific bookList inside of your function.

You are close here. It’s just a matter of only using the variables declared as function arguments and never directly using the global variables themselves.

1 Like

Yes, I get it know, I just needed to enter a landing arr (as parameter) to both add() and remvoe() so that these could hold the elements of bookList and be transformed to a newArr when bookList is entered as a parameter…

I’m confused now. Is your code working? If so, yay.

Yes it does, but I think I over complicated it, (as you can read from the explanation of my own solution) just like pretty much everything I tend to do on Js, but hey, you’ve been a great guide and I really appreciate your disposition (and patience… with me) :rofl:

1 Like

Oh, it takes a long time to get comfortable pairing code down to less complicated stuff. I’m still working on it myself. It’s a lifelong process.

2 Likes