Build a Symmetric Difference Function - Build a Symmetric Difference Function

Tell us what’s happening:

I am having issues where one array is greater in length than the other array for this Lab. It doesn’t seem to include the exclusive element within the larger array if one array is shorter than the other.

Your code so far

function diffArray(arrayA, arrayB) {
  const newArr = [...arrayA];
  newArr.push([...arrayB]);

  let filteredArr = [];

  console.log("Combined Array: " + newArr + "\n");

  
  filteredArr = newArr.filter(element => arrayA.includes(element) !== arrayB.includes(element));

  console.log("Filtered Array: " + filteredArr + "\n");

  return filteredArr;
}

console.log(diffArray(["pen", "book"], ["book", "pencil", "notebook"]));

I was also thinking of alternative approaches. One of which checking if an element repeated more than once in the combined array, so that would be newArr. However, I don’t know how you would check such a value without a running sum or using the map data structure (which stores key value pairs). There is also the possibility of using a set, so it only stores exclusive values from both arrays but I assume the Lab doesn’t want this approach. Especially since map and set haven’t been explored in the modules.

Console Output:

Combined Array: pen,book,book,pencil,notebook

Filtered Array: pen

[ 'pen' ]

Challenge Information:

Build a Symmetric Difference Function - Build a Symmetric Difference Function

what are you doing here?
check the value of newArr with console.log(newArr)

1 Like

I’m trying to push all the elements from arrayB onto the array which is stored in the newArr variable. I logged what newArr has stored to the console before I utilized the .filter() method.

console.log("Combined Array: " + newArr + "\n");

Output:

Combined Array: pen,book,book,pencil,notebook

don’t do that, just console.log(newArr) or you will miss a thing

1 Like

It looks like your filter logic is actually on the right track, but take a closer look at how you are combining your arrays at the start.

When you use .push([...arrayB]), you might be adding the entire second array as a single nested element inside newArr instead of merging the individual items. Try checking the console.log of your newArr to see if it’s truly a flat list of items or if there’s an array inside an array.

1 Like

Oh…that makes much more sense as to why its not working.

[ 'pen', 'book', [ 'book', 'pencil', 'notebook' ] ]

Okay, I altered my approach and utilized the .concat() method instead and that worked. Thank you for bringing that to my attention!

Yeah, I just realized my mistake. I thought you could combine the elements of arrays using this approach, but I was wrong. I utilized the .concat() method instead. Thank you!

1 Like

you can

notice how you are pushing an array here? how to push elements without pushing an array? do you think you can change that line to obtain that?

1 Like

I already submitted the lab, so I am unable to experiment in the console with the code. However, would it be possible to omit the square brackets here…? Maybe that would avoid pushing the entire array as a singular element and instead push the rest of the elements of arrayB.

Example:

newArr.push(…arrayB)

you can always experiment, you can use the editor for other things than the labs, or you can open the browser console

try to join these two using push:

const a = [1,2,3];
const b = [4,5,6];
1 Like

Plenty fair! I opened up the browser console and initialized the two arrays you provided. I pushed array b onto array a and got the following:

It returns 6 since .push() and .unshift() return the length of the newly modified array. Then it appears to show array a with the elements from its initial state along with the elements added from array b. So, it appears if you omit the square brackets then it would push each individual element from the secondary array rather than the entire array as a singular element if you included them.

so now you know the correct method to put the two arrays together using push!

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.