2

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?

3
  • You have 2 calls to remove. Which one is giving you that error? Commented Apr 25, 2017 at 1:52
  • It says it's happening at the remove function. Jsfiddle points to let newArr = bookList.slice(); Commented 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. Commented Apr 25, 2017 at 2:12

2 Answers 2

5

Your problem is Array.push

return The new length property of the object upon which the method was called.

You should return array instead

function add (bookListTemp, bookName) {
      let newBookArr = bookListTemp;
      newBookArr.push(bookName);
      // Add your code above this line
      return newBookArr;
    }

OR Let's try Array.concat instead

function add (bookListTemp, bookName) {
  let newBookArr = bookListTemp;
  return newBookArr.concat(bookName);
  // Add your code above this line
}
Sign up to request clarification or add additional context in comments.

5 Comments

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.
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. :)
unfortunately I cannot get rid of the add function for the challenge :/
OK. So if you want to you push, return newBookArr instead of newBookArr.push - see my edit post :)
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?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.