Symmetric Difference**

Tell us what’s happening:
How can I filter the elements which are equal to the elements which are equal to one element in my newArr
Your code so far


function sym(...args) {
  let newArr=[];
  let eins=0;
  let zwei=0;
  let drei=0;
  let vier=0;
  let funf=0;
  let sechs=0;
  let sieben=0;
  let acht=0;
  let neun=0;

  for(let i = 0;i<args[1].length;i++){
             args[0].push(args[1][i])
  }

  for(let i = 0;i<args[0].length;i++){
          if(args[0][i]==1){
            eins+=1
          }
          if(args[0][i]==2){
            zwei+=1
          }
          if(args[0][i]==3){
            drei+=1
          }
          if(args[0][i]==4){
            vier+=1
          }
          if(args[0][i]==5){
            funf+=1
          }
          if(args[0][i]==6){
            sechs+=1
          }
          if(args[0][i]==7){
            sieben+=1
          }
          if(args[0][i]==8){
            acht+=1
          }
          if(args[0][i]==9){
            neun+=1
          }
             
          }
if(eins==1){
   newArr.push(1)

}
if(zwei==1){
   newArr.push(2)

}
if(drei==1){
   newArr.push(3)
}
if(vier==1){
   newArr.push(4)
}
if(funf==1){
   newArr.push(5)
}
if(sechs==1){
   newArr.push(6)
}
if(sieben==1){
   newArr.push(7)
}
if(acht==1){
   newArr.push(8)
}
if(neun==1){
   newArr.push(9)
}

  for(let i=2;i<args.length;i++){
       for(let j=0;j<args[i].length;j++){
          if(newArr[j]!=args[i][j]){
            newArr.push(args[i][j])
         }else{
           newArr.filter(e=>e==args[i][j])//I have the problem here
         }
  
           }
      
  }
  
 
 
}

sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]);
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.72 Safari/537.36.

Challenge: Find the Symmetric Difference

Link to the challenge:

Hello there,

I am not really going to answer your question, because I assume you want to be able to pass the tests, which means you will need to change a lot more of your code than this one line.

You have far too much hard coding in your script. So, I am not sure if you plan on keeping most of it, but I suggest you reset the code, and start again.


To this particular line:

The filter method does not alter the array. It returns a new array. So, this line is just returning a new array, and doing nothing with it.

Also, your function does not return anything. So, it will definitely not pass the tests.

I suggest you start with that, and return with updated code, and a new question. Otherwise, I recommend going through the JavaScript section of the curriculum once more.

Hope this helps

1 Like

I think that it could be this way. But, how can I eliminate the duplicates in every array in args?

function sym(...args) {
  let firstGroup=args[0];
  let secondGroup=args[1];
  let pivotGroup=[];
  let lengthOfargs=args.length
  //console.log(lengthOfargs)
  
   for(let i = 0;i<firstGroup.length;i++){
      if(!secondGroup.includes(firstGroup[i]) && !pivotGroup.includes(firstGroup[i])){
        pivotGroup.push(firstGroup[i])
     }
   }
   for(let i = 0;i<secondGroup.length;i++){
      if(!firstGroup.includes(secondGroup[i]) && !pivotGroup.includes(secondGroup[i])){
        pivotGroup.push(secondGroup[i])
      }
   }
    
//   return pivotGroup
  
  
  
    
    for(let i=2;i<args.length;i++){
        for(let j=0;j<args[i].length;j++){
              if(pivotGroup.includes(args[i][j])){
                   
                    pivotGroup=pivotGroup.filter(e=>e!=args[i][j]) 

                      }else{
                        pivotGroup=pivotGroup.push(args[i][j])         
                      }     
            }
      

       }
    
     
      console.log(pivotGroup)
      //return pivotGroup
   
}

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

I am seeing errors in the console, because of this line:

pivotGroup=pivotGroup.push(args[i][j])

It is not doing what you expect. Here is the documentation on .push:


Also, what do you expect this to do:

function sym(...args) {
  let firstGroup=args[0];
  let secondGroup=args[1];

What about a 3rd group? Or a 4th group?

1 Like

How can I do with those groups? Because they make the pivotGroup, the functions works with that.
What can I do to elimanate the duplicate? I jave just read methods that employs “hard code”

I think I solved the problem with push.

function sym(...args) {
                  
  let firstGroup=args[0];
  let secondGroup=args[1];
  let pivotGroup=[];
  let lengthOfargs=args.length;
  //console.log(lengthOfargs)
   if(lengthOfargs==2){
     for(let i = 0;i<firstGroup.length;i++){
      if(!secondGroup.includes(firstGroup[i]) && !pivotGroup.includes(firstGroup[i])){
        pivotGroup.push(firstGroup[i])
     }
   }
   for(let i = 0;i<secondGroup.length;i++){
      if(!firstGroup.includes(secondGroup[i]) && !pivotGroup.includes(secondGroup[i])){
        pivotGroup.push(secondGroup[i])
      }
   }
    //console.log(pivotGroup)
 //return pivotGroup

   }else{
      for(let i=2;i<args.length;i++){
        for(let j=0;j<args[i].length;j++){
              if(pivotGroup.includes(args[i][j])){                                  
                  pivotGroup=pivotGroup.filter(e=>e!=args[i][j]) 
                    }else{
                        pivotGroup.push(args[i][j])         
                      }     
            }
      }
      //console.log(pivotGroup)
       //return pivotGroup
   }

  }

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

I finished it, but how can I refactor it?

function sym(...args) {
   let newArgs=[];
   for(let i=0;i<args.length;i++){
                 newArgs.push(args[i].filter((a, b) => args[i].indexOf(a) === b))
   }
   let firstGroup=newArgs[0];
   let secondGroup=newArgs[1];
   let pivotArr=[];
   let lengthOfArrs=args.length;
   //console.log(newArgs)     
        if(lengthOfArrs==2){
          for(let i=0;i<secondGroup.length;i++){
             if(!firstGroup.includes(secondGroup[i]) && !pivotArr.includes(secondGroup[i]) ){
                             pivotArr.push(secondGroup[i])
             } 
      }
         for(let i=0;i<firstGroup.length;i++){
             if(!secondGroup.includes(firstGroup[i]) && !pivotArr.includes(firstGroup[i])){
                            pivotArr.push(firstGroup[i])
               }
        }
         
         return pivotArr.sort((a,b)=>a-b)                 
      }else{
        
        for(let i=0;i<secondGroup.length;i++){
             if(!firstGroup.includes(secondGroup[i]) && !pivotArr.includes(secondGroup[i]) ){
                             pivotArr.push(secondGroup[i])
             } 
      }
      
         for(let i=0;i<firstGroup.length;i++){
             if(!secondGroup.includes(firstGroup[i]) && !pivotArr.includes(firstGroup[i])){
                            pivotArr.push(firstGroup[i])
               }
        }
            for(let i = 2;i<newArgs.length;i++){
                  for(let j=0;j<newArgs[i].length;j++){
                        if(pivotArr.includes(newArgs[i][j])){
                          pivotArr=pivotArr.filter(e=>e!=newArgs[i][j])
                    }         else{
                                pivotArr.push(newArgs[i][j])
                    }
              }
          } 
          
      }
           //console.log(pivotArr.sort((a,b)=>a-b))
            return pivotArr.sort((a,b)=>a-b)
    }

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

Well done, on completing this.

There are many ways this could be done. To get a better idea of improvements, I suggest you look at the guide post: freeCodeCamp Challenge Guide: Find the Symmetric Difference - Guide - The freeCodeCamp Forum

Hope this helps

2 Likes