Is the hint wrong on "Refactor Global Variables Out of Function"s

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

So I solve this problem correctly but I feel like the hint in the prompt misled me. It says:

Hint: using something like const newArr = arrVar , where arrVar is an array will simply create a reference to the existing variable and not a copy. So changing a value in newArr would change the value in arrVar .

When I tried something like this I always failed the first test. But it works fine once I figured out that I need to use the spread operator to copy the list over instead of only the ‘=’

  **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(list, bookName) {

let newBookList = [...list];

newBookList.push(bookName);
return newBookList;

// Change code above this line
}

// Change code below this line
function remove(list,bookName) {
const book_index = bookList.indexOf(bookName);
const newBookList = [...list];

if (book_index >= 0) {

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

  // Change code above this line
  }
}
  **Your browser information:**

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

Challenge: Refactor Global Variables Out of Functions

Link to the challenge:

I’m not sure I understand. The hint tells you that you cannot copy the array via the syntax

Correct, but the example the hint gives you about how to get around this problem doesn’t work.

I don’t see an example in the hint of how to get around it. The hint just tells you what not to do. It doesn’t tell you how to do it. Unless I’m missing something.

1 Like

Hint: using something like **const newArr = arrVar**

That’s not the entire hint, you are taking it out of context. The hint is:

“Hint: using something like const newArr = arrVar, where arrVar is an array will simply create a reference to the existing variable and not a copy. So changing a value in newArr would change the value in arrVar.”

This hint is telling you not to do something like const newArr = arrVar. But it doesn’t tell you what to do.

2 Likes

Thank you! I understand now.

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