Symmetric Difference*

Hello!, I want to push the elements of the next array in the args in the newArr.

I could filtered the newArr,

But, How can I introduce the elements which are not included in the newArr.

function sym(...args) {

  let newArr=[];

  let arr0=args[0];

  let arr1=args[1];

  let lengthOfArgs=args.length;

        if(lengthOfArgs==2){

            for(let i=0;i<arr1.length;i++){

            if(!arr0.includes(arr1[i]) && !newArr.includes(arr1[i]) ){

                newArr.push(arr1[i])

            }

        }

        

        for(let i=0;i<arr0.length;i++){

            if(!arr1.includes(arr0[i]) && !newArr.includes(arr0[i]) ){

                newArr.push(arr0[i])

            }

        }

        return newArr.sort((a,b)=>a-b)

        }else{

            for(let i=0;i<arr1.length;i++){

            if(!arr0.includes(arr1[i]) && !newArr.includes(arr1[i]) ){

                newArr.push(arr1[i])

            }

        }

        

        for(let i=0;i<arr0.length;i++){

            if(!arr1.includes(arr0[i]) && !newArr.includes(arr0[i]) ){

                newArr.push(arr0[i])

            }

        }

        for(let i=2;i<args.length;i++){

                for(let j=0;i<newArr.length;j++){

                        if(args[i].includes(newArr[j])){

                                newArr=newArr.filter(e=>!args[i].includes(e))

                        }else{

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

                        }

                        //if(!newArr.includes(args[i][j])){

                        //        newArr.push(newArr[i][j])

                        //}

                }

                

            }

        }

       

}

sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]);

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Why is this not working?

function sym(...args) {

  let newArr=[];

  let arr0=args[0];

  let arr1=args[1];

  let lengthOfArgs=args.length;

        if(lengthOfArgs==2){

            for(let i=0;i<arr1.length;i++){

            if(!arr0.includes(arr1[i]) && !newArr.includes(arr1[i]) ){

                newArr.push(arr1[i])

            }

        }

        

        for(let i=0;i<arr0.length;i++){

            if(!arr1.includes(arr0[i]) && !newArr.includes(arr0[i]) ){

                newArr.push(arr0[i])

            }

        }

        return newArr.sort((a,b)=>a-b)

        }else{

            for(let i=0;i<arr1.length;i++){

            if(!arr0.includes(arr1[i]) && !newArr.includes(arr1[i]) ){

                newArr.push(arr1[i])

            }

        }

        

        for(let i=0;i<arr0.length;i++){

            if(!arr1.includes(arr0[i]) && !newArr.includes(arr0[i]) ){

                newArr.push(arr0[i])

            }

        }

        for(let i=2;i<args.length;i++) {
            for(let j=0;j<newArr.length;j++){
                  if(newArr.includes(args[i][j])){
                    newArr = newArr.filter(val => !args[i].includes(val))
                  }else{
                    
                    newArr.push(args[i][j])
                    
                  }
            }

        }

        }

       
console.log(newArr)
}

sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]);

[/quote]