Getting TypeError in FCC for code which works perfectly well in [Solved]

While doing Seek and Destroy I got a “TypeError: newArr[0].filter is not a function” error message in FCC for the code copied from me code which worked “perfectly”:

function destroyFilter(value) {
  //comp = 5;
  return value !== comp;
function destroyer(arr){
  for (i=1; i < arr.length /*"no of arguments - 1"*/;i++){
  comp = arr[i];
  //  console.log(comp);
  newArr = arr[0].filter(destroyFilter);
    arr[0] = newArr;
 return newArr;

destroyer([ [1,2,3,4,5,8,9,3,10],3,4,5]);

Usually I have unhappy reactions when I don’t declare var i = 0 in the for loop.
Also would you mind putting your entire code? You call a destroyFilter function in your filter() but we can’t see it. And after that other people will answer coz I’m out of my depth :slight_smile:

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

First - What is destroyFilter?

But the bug is that you have one argument arr for your function, which is an array and then you try to call .filter() on the first element of that array, which is a number, and numbers don’t have .filter() method.

The .filter() method is only applicable on Arrays.
In your case, your applying to the first element of the array, not the array itself.

The docs on MDN

@IOAyman @jenovs arr[0] looks to be an array to me, judging by the function call at the bottom.

@RayHamilton its difficult to see what the problem is without seeing destroyFilter(). I can’t see why it would work on CP but not FCC. This is probably a redundant question, but did you remember to also copy the destroyFilter() function to the FCC editor?

arr[0] is a number.
arguments[0] would be an array :wink:

Sorry, could you explain that to me? I thought that a two dimensional array would be treated as a single argument? Or am I way off the mark there?

Ups :blush:

Didn’t notice it was a two dimensional array in his example. My bad.

FCC example has an array + additional arguments, like this:

destroyer([1, 2, 3, 1, 2, 3], 2, 3)

I have put the code that I didn’t copy in the post, but I did copy it to FCC!

Did you mean that should have used the reserved word “var” in the loop?

If you look at the code, the first element of the array is an array itself, so it should still work?

@JavaTheNutt, @RayHamilton You’re right, I missed that.

Like I said I don’t know much at all, only that when I do that in repl/it I get a message because it declares a global variable, but you only need i in your loop… Have the other answers helped you?

I appreciate your input but I was mostly asking why the code WORKS at (I know it is not brilliant but I was just trying to get something that would do something anywhere near correctly, and then I would improve it) but the SAME code doesn’t even run at FCC, let alone produce an answer, No one has explained this yet. I am close to getting a correct solution.

1 Like

Your code and code on FCC has different arguments in function call. You have an array, fcc has array and arguments.

Sorry I think you mistake my problem. The code that I posted WORKS at - it runs and produces an answer (not quite what the FCC problem answer is expecting I know, it is a work in progress) but the SAME code does not even run at FCC, let alone produce an answer. What is going on?

You call your code with:

destroyer([ [1,2,3,4,5,8,9,3,10], 3, 4, 5]);

fcc calls your code with:

destroyer([1,2,3,4,5,8,9,3,10], 3, 4, 5);

See the difference? Your code has an array with parameters; fcc’s code has parameters and first is an array.

1 Like

You are right! I am going to go and get myself some glasses! (I originally meant spectacles, but now I am thinking of a few beers - it has been too long a day!) It is often the simple mistakes that are hardest to spot!

Thanks jenovs