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 codepen.io code which worked “perfectly”:
function destroyFilter(value) {
//comp = 5;
return value !== comp;
}
function destroyer(arr){
console.log(arr.length);
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;
console.log(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
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.
@IOAyman@jenovsarr[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?
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 codepen.io (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.
Sorry I think you mistake my problem. The code that I posted WORKS at codepen.io - 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 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!