Symmetric Difference array type error

Symmetric Difference array type error
0.0 0

#1

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:


#2

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.


#3

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.


#4

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 .


#5

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.


#6

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!


#7

I do appreciate your help as well!