Symmetric Difference array type error

Tell us what’s happening:

When I run my code I get the following error:
TypeError: [object Array] is not a function

All of the documentation I can find on reduce says the fourth parameter of the callback function should be the array reduce is called on. So there shouldn’t be a problem but this appears to be causing the trouble.

Your code so far

function sym() {
  
  var all = Array.prototype.slice.call(arguments);
 
  return all.reduce(function(accum,ele,ind,arr){
    if(arr.findIndex(ele) == arr.lastIndexOf(ele))
      return accum.append(ele);
    else
      return accum;
  }, []);
  
 }

sym([1, 2, 3], [5, 2, 1, 4]);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Linux; Android 8.1.0; Pixel XL Build/OPM1.171019.016) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.137 Mobile Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/symmetric-difference

It looks like you’re using the prototype for Array.slice, not the code.

What’s “ele”? It’s not declared anywhere and that seems to be what’s holding up your code. accum and ind also don’t appear to be declared.

1 Like

Thanks for your response. The prototype is used to turn the arguments object into an array. The variables you mentioned are all in the function call back parameter list.

Let’s work through your test case to see why you would be getting that error.

If we call your function like:

sym([1, 2, 3], [5, 2, 1, 4]);

After the first line of code, the variable all looks like below (an array containing two elements which are arrays).

[[1, 2, 3], [5, 2, 1, 4]]

Inside the reduce callback function, the left part of your if condition is:

arr.findIndex(ele)

This is what causes your error. You need to read through the findIndex documentation carefully as the first parameter is supposed to be a function. Instead, you have supplied an array [1, 2, 3], which is is causing the TypeError: [object Array] is not a function .

1 Like

I just spent the last half hour researching this and when I finally find the answer and go to post it, somebody beat me to it. :frowning:

I gotta be quicker on the draw.

1 Like

Thank you so much for your help! I modified and remodified and made special JavaScript scripts just to test individual aspects of the code. Never even considered that find would take anything other than a value(which is what I was passing in rather than the array in the current iteration of the code). This functional programming is really driving me nuts. Really miss loops but supposedly these are the “advanced solutions.” Thanks again!

I do appreciate your help as well!