Pls I don't know why the "newestBookList" is not working correctly?

Tell us what’s happening:
Describe your issue in detail here.

  **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
let copyBookList = [];
function add (copyBookList, bookName) {
 copyBookList = bookList.slice(0, bookList.length)
copyBookList.push(bookName);
return copyBookList;

// Change code above this line
}

// Change code below this line
function remove (copyBookList, bookName) {
 copyBookList = bookList.slice(0, bookList.length)
var book_index = copyBookList.indexOf(bookName);
if (book_index >= 0) {

  copyBookList.splice(copyBookList.indexOf(bookName), 1);
  return copyBookList;

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

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36

Challenge: Refactor Global Variables Out of Functions

Link to the challenge:

You have some issues with your variables.

let copyBookList = [];
function add (copyBookList, bookName) {
  copyBookList = bookList.slice(0, bookList.length)

Why are you declaring the variable copyBookList globally, passing it in as a parameter, and then overwriting it with a sliced version or the global variable bookList.

Your functions should not use global variables (almost always a bad idea). And you should not change the variable passed in as a parameter. You should use the array passed in, make a copy of it, and do your work on that copy. Then you return that copy.

Do not reference any global variables in your functions. Do not change the parameters that were passed in.

When I fix some of your variable problems, your code passes for me. So, the idea of how you’re handling these modifications is sound, it’s just that you are confusing your variables.

1 Like

Thanks bro, it’s working perfectly now.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.