The use of ...arr


How does newArr copied the booklist? “[…arr]” = bookList as it is a global variable?
Why there is no “newArr.indexOf(bookName) <0 or === -1” in the add function to see if the bookName exists in the bookList already?
Anybody can help me with this? Thanks!

Please post your code instead of a picture. Also please post a link to the challenge. Thanks

Thanks, please check my new post : How can I understand the use of […arr]

Anybody can tell me how to understand the line of “let newArr = […arr]”? The comment says that:// Copy the bookList array to a new array. but what I want to say is that arr is not defined yet, why could it pass its properties and values to newArr? How?
Thanks!

Queation:
Rewrite the code so the global array bookList is not changed inside either function. The add function should add the given bookName to the end of the array passed to it and return a new array (list). The remove function should remove the given bookName from the array passed to it.

Note: Both functions should return an array, and any new parameters should be added before the bookName parameter.

  **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(arr, bookName) {
let newArr = [...arr]; // Copy the bookList array to a new array.
newArr.push(bookName); // Add bookName parameter to the end of the new array.
return newArr; // Return the new array.
} 
// Change code above this line

// Change code below this line
function remove(arr, bookName) {
let newArr = [...arr]; // Copy the bookList array to a new array.
if (newArr.indexOf(bookName) >= 0) {
  // Check whether the bookName parameter is in new array.
  newArr.splice(newArr.indexOf(bookName), 1); // Remove the given paramater from the new array.
  return newArr; // Return the new array.
  // Change code above this line
  }
}
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36

Challenge: Refactor Global Variables Out of Functions

Link to the challenge:

Why would you say that arr is not defined yet? It is passed into the function as an argument.

The starting code is a bit odd as it is missing the array parameter.

Only if reading the challenge text carefully…

The add function should add the given bookName to the end of the array passed to it and return a new array (list).

…or if you look at the test output to see how the functions are called…

function add(bookName) {
  ...starting code
}

add(bookList, "A Brief History of Time")

…would you know to add the parameter.


Edit: not that you have to have two parameters.

You can do some rest/spread with just one parameter.

function doSomething(...args) {
  return [...args[0], args[1]]
}

You can also ignore the first argument and spread the top-level bookList array.

function doSomething(_, someName) {
  return [...someList, someName]
}

I changed the identifiers but it is still a spoiler so the code is blurred as well.

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