相关文章推荐
失落的炒粉  ·  C# ...·  8 月前    · 
豪气的板栗  ·  最新版Digital ...·  1 年前    · 
儒雅的煎饼  ·  ubuntu16.04+cuda8.0安装p ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Im encountering an odd problem when going through freeCodeCamp beta.

The "purpose" of this is not modifying the original array and using functional programming techniques to modify arrays.

However I keep getting complaints about the "array" parameter is the remove function not being a valid function:

//  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 (bookListTemp, bookName) {
  let newBookArr = bookListTemp;
  return newBookArr.push(bookName);
  // 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 (bookList,bookName) {
  let newArr = bookList.slice();
  if (newArr.indexOf(bookName) >= 0) {
    return newArr.slice(0, 1, bookName);
    // 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');
console.log(bookList);

In the remove function i've tried taking the parameter and doing array.slice() method as well as array.concat() method. Since doing let newArr = bookList doesn't actually make a new array correct? it just makes a new copy that references the original array correct?

The exact error I get is TypeError: bookList.slice is not a function

What's even weirder is Array.isArray(bookList) returns true (in the function remove. So I don't understand why it's complaining about array methods?

It says it's happening at the remove function. Jsfiddle points to let newArr = bookList.slice(); – msmith1114 Apr 25, 2017 at 2:05 Your functions aren't returning the array. [].push() does not return the array, it returns the resulting length. [].slice() also does not return the full array. – 000 Apr 25, 2017 at 2:12
function add (bookListTemp, bookName) {
      let newBookArr = bookListTemp;
      newBookArr.push(bookName);
      // Add your code above this line
      return newBookArr;
Let's try Array.concat instead

function add (bookListTemp, bookName) {
  let newBookArr = bookListTemp;
  return newBookArr.concat(bookName);
  // Add your code above this line
                Doesn't setting one array equal to the other just make a copy reference of the original...so if I end up changing this copy won't it mess up the original.
– msmith1114
                Apr 25, 2017 at 2:18
                Yes. Because concat return new A new Array instance. So in my opinion, you don't need funtion add. Just using concat. And MDN is good document to reference. :)
– taile
                Apr 25, 2017 at 2:21
                OK. So if you want to you push, return newBookArr instead of newBookArr.push - see my edit post :)
– taile
                Apr 25, 2017 at 2:40
                Ended up fixing it by making sure to do .slice() into new arrays on each one. So when we normally copy an array like let arrnew = oldarray. if we change arrnew will it always change oldarray?
– msmith1114
                Apr 25, 2017 at 2:44

There are two ways to copy the array without mutating it. You will not be able to use the .slice() method on the bookList, because it is an argument in the function and therefore not an array. The work around is var newBookArr = Array.prototype.slice.call(bookListTemp); or [].slice.call(bookListTemp);

This allows you to perform the slice on the bookList when it is an argument. The other way I discovered, when playing around with it - var newBookArr = [].concat(bookListTemp);

When trying var newBookArr = [].push(bookListTemp); we find the original bookList pushed inside the new array. So it is a copy, but as an array within an array. the .concat() method merges the old array into the new.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.