javascript - Array.slice() parameter is not a function - Stack Overflow

Im encountering an odd problem when going through freeCodeCamp beta.The "purpose" of this is

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 plaints 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 e 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 e 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 plaining about array methods?

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 plaints 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 e 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 e 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 plaining about array methods?

Share Improve this question edited Apr 25, 2017 at 2:10 msmith1114 asked Apr 25, 2017 at 1:43 msmith1114msmith1114 3,2599 gold badges49 silver badges105 bronze badges 3
  • You have 2 calls to remove. Which one is giving you that error? – Manngo Commented Apr 25, 2017 at 1:52
  • It says it's happening at the remove function. Jsfiddle points to let newArr = bookList.slice(); – msmith1114 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. – 000 Commented Apr 25, 2017 at 2:12
Add a ment  | 

2 Answers 2

Reset to default 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
}

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744131953a4559883.html

相关推荐

  • javascript - Array.slice() parameter is not a function - Stack Overflow

    Im encountering an odd problem when going through freeCodeCamp beta.The "purpose" of this is

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信