Sorted Union - Doubt with my code

Sorted Union - Doubt with my code
0

#1

Tell us what’s happening:

Can someone tell why this console.log() printed in the console the sorted array (The one the challenge asked) even though it’s at the beginning and it should’ve printed the parameters given (The last line of my code), and why the push function adds it as if it were a new array and not to the array given as a parameter.
??? I’m very confused

Your code so far

  console.log(arr);
  //var arr2 = [];
  for(var i=0; i<arguments.length; i++){
    for(var j=0; j<arguments[i].length; j++){
      if(!arr.includes(arguments[i][j])){
        arr.push(arguments[i][j]);
      } 
    }
  }
  return arr;
}

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

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

**Link to the challenge:**
https://www.freecodecamp.org/challenges/sorted-union

#2

You are missing the function declaration and any other code you may have above console.log(arr). Please post your entire code. Use the </> button in the editor and the paste your code between the 2 sets of backticks.


#3

Sorry, Here it is:

function uniteUnique(arr) {
  console.log(arr);
  //var arr2 = [];
  for(var i=0; i<arguments.length; i++){
    for(var j=0; j<arguments[i].length; j++){
      if(!arr.includes(arguments[i][j])){
        arr.push(arguments[i][j]);
      } 
    }
  }
  return arr;
}

//uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);
uniteUnique([1, 3, 2], [1, [5]], [2, [4]]);

#4

In the test case of:

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

There are 3 arguments passed into the function. The function has only one parameter (arr), so when you put console.log(arr) at the beginning of the function arr accepts the first argument passed in. So the console.log(arr) results in [1,3,2] being displayed.

If your function would have defined two 2 parameters for the function as in:

function unitUnique(arr1,arr2) {

then if you would have put console.log(arr1,arr2), it would have displayed [1,3,2] [1, [5]], because those are the first two arguments passed to the function.

The purpose of this challenge is to teach you how to create a function which can handle an unknown number of arguments passed in (arrays in this case) and perform a specific task with those arguments.