Refactor Global Variables Out of Functions Using the Spread Operator

Tell us what’s happening:

my question is…why does my code fail without the spread operator while this :
function Vfun(Array)
let A2=Array

works without the spread operator ?

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"];

/* This function should add a book to the list and return the list */
// New parameters should come before the bookName one

// Add your code below this line
function add (Library,bookName) {
  let AddedArray=[...Library]
   return AddedArray;
  // Add your code above this line

/* This function should remove a book from the list and return the list */
// New parameters should come before the bookName one
// Add your code below this line

function remove (Library,bookName) {
  if (Library.indexOf(bookName) >= 0) {
    let RemovedArray=[...Library]
    RemovedArray.splice(Library.indexOf(bookName), 1);
    return RemovedArray;
    // Add your 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');


Your browser information:

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

Link to the challenge:

What kind of error do you get ?

Also, could you paste your code somewhere else than in a spoiler so that we can copy it ? Because of the way freecodecamp’s forum works it’s tedious to copy a code that’s inside a spoiler

the code under the spoiler works fine as long as it has the spread operator and because of that,i’ve been advised before to use the spoiler if “my code works or almost works”.when i don’t use the spread operator my arrays are different than the result i’m supposed to produce.I removed the spoiler.

Oh, ok didn’t realise the code was working fine.

In the exercise, the spread operator is used to copy an array. In javascript you cannot create directly a copy of an array and modify one or the other, you have to use some tricks or use the spreak operator, as advised in the exercise.

For exemple, if you were to create something like this :

var original = [1,2,3,4,5]
var copy=original // (faulty) var copy = [1,2,3,4,5]

both the original array and its copy would display [1,2,3,4,5,6], you can try it yourself.

So, in the exercise the spread operator is used to create a new array whose elements are the same as the original array. This way you can modify it at will without having to also modify the library array. If you were to use direcly the Library array without bothering to create a copy of it, you would modify the original array everytime you’d call the function .

Hope that awnsers your question !

Edit : messed up the code, I corrected it but left what I originally wrote commented out

I tried your code on both VS and JS Bin and they turned out to be different in both Capture

Yeah, that’s me who’s stupid and is doing two things in the meantime. Here’s what the code was supposed to be like ;

var original = [1,2,3,4,5]
var copy = original

so although this method does work with variables…it doesn’t work with arrays right ?