Cant access array withing a for loop

Tell us what’s happening:

instead of getting specified argument it is getting an element within the array

Your code so far


function sym() {
var dif=[];
var itr;
for(var i=0; i<arguments.length; i++){
  console.log("="+arguments[0]);
  itr=0;
  arguments[i].forEach(function(elem){
    for(var j=1; j<arguments.length; j++){
      console.log("==="+arguments[j]);//"i "+i//
      if(!arguments[j].includes(elem)){
        itr++;
      }
    }
    if(itr==arguments.length) dif.push(elem);
  });
}

dif=[...new Set(dif)];//make unique
console.log(dif);
return dif.sort();
}

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36.

Challenge: Find the Symmetric Difference

Link to the challenge:

you are passing two arguments to the function
argument[0] = [1, 2, 3]
argument[1] =[5, 2, 1, 4]
so
function sym() {
for(var i=0; i<arguments.length; i++){
arguments[i].forEach(function(elem){console.log(elem)});
}
will log
1
2
3
5
2
1
4

2 Likes

but i need to get the unique value of each array not the unique values of all arrays as one.

I still don’t understand why arguments[1].include() fails because it thinks its an element not an array

arguments should work like a multidimensional array here

sym([1, 2, 3], [5, 2, 1, 4]);
function sym() {
for(var i=0; i<arguments.length; i++)
console.log(arguments[i]);
}

this will log
(3) [1, 2, 3]
(4) [5, 2, 1, 4]


I think i understand what im doing wrong, but unfortunately i have no idea how to solve this problem.
i thought there unspecified number of arrays but one of the hint answers seems to use only two.

at one point i was getting mostly correct using something like this

args1.forEach(function(elem){
if(!args2.includes(elem)){
dif.push(elem);
}
});
args2.forEach(function(elem){
if(!args1.includes(elem)){
dif.push(elem);
}
});

dif=[…new Set(dif)];//make unique
console.log(dif);
return dif.sort();

OKay

lets get started on the right foot
The instructions say …

Create a function that takes two or more arrays
according to the instructions
you find the dif between the first two arrays
then you must retain that result
if there is no third array then
just return the stored result
if there is a third array then
you must find the difference between the third
array the stored result
then store the new result
if there is no fourth array then
just return the stored result
if there is a fourth array then
you find the difference between the fourth
array and the stored array
and so on
so lets start like this

First

function sym() {
     var dif = [...new Set(arguments[0].slice())];
     var arg;

here we are making a NewArray populated
by the first argument with any duplicate
values removed
dif will be the storage array
arg will be used for convenience
now we must get the second array
and any other arrays that might be
in arguments

for(let i=1;i<arguments.length;i++){
  		arg = [...new Set(arguments[i])];

the for loop starts with i=1 because
we already have arguments[0]
arg is used to point to the array we are
currently working with that will be
compared to the stored array

1 Like

Yes i was just thinking about it wrong. There is no need to compare each array with every other array directly just compare the difs of any two until the unique ones of every pair have been diffed.

please read my post again
i have added to it
if you like I can continue
all the way thru my own
working solution
please post here if
you wish me to continue

1 Like

I’m not really sure if this what you were doing but this works

function sym() {
  var dif = [...new Set(arguments[0].slice())];
  var arg = [...new Set(arguments[1].slice())];
  var result=[];
  dif.forEach(function(elem){
    if(!arg.includes(elem)) result.push(elem);
  });//initial comparison both ways
  arg.forEach(function(elem){
    if(!dif.includes(elem)) result.push(elem);
  });

  for(let i=2;i<arguments.length;i++){
  	arg = [...new Set(arguments[i].slice())];
    arg.forEach(function(elem){
      if(!result.includes(elem)){
        result.push(elem);
      }else{
        delete result[result.indexOf(elem)];
      }
    });
  }
  return result.filter(e => e); //remove empty elem
function sym() {
 	var arg,dif = [...new Set(arguments[0])];
 	for(let i=1;i<arguments.length;i++){
  		arg = [...new Set(arguments[i])];
  		for(let i=0; i<arg.length; i++){
    			if(dif.includes(arg[i])){
      				dif.splice(dif.indexOf(arg[i]),1)
    			}else{  
      				if(!dif.includes(arg[i])){
        				dif.push(arg[i]);
      				}
    			}
  		} 
 	} 
  	dif = [...new Set(dif)];
	return dif.sort();
}

I will explain each line I you ask

thats the best concise way to do. the biggest thing i was missing was
= [...new Set(arguments[i]]
for reasons it doesnt let you operate on argument itself
is it a mutable issue

Sorry, posted before thinking

now i understand better

https://medium.com/front-end-weekly/es6-set-vs-array-what-and-when-efc055655e1a

i couldnt find any articles on this in freecodecamp