Review my code Sorted Union

Review my code Sorted Union
0

#1

Is it nice ? or can be improved ?


function uniteUnique(arr,...arg) {
for(let i in arg)
{
  arr.push(...arg[i]);
}
for(let i=0;i<arr.length;i++)
{
  for(let j=i+1;j<arr.length;j++)
{
  if(arr[j]==arr[i])
  {
    arr.splice(j,1);
  }
}

}
return arr
}
console.clear();
console.log(uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]));

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sorted-union


#2

Hi!

Two things: use tabulation and spaces between operators. Also it’s a nice habit to comment your code:

/*******************************
* Description of the function  *
*******************************/
function uniteUnique(arr, ...arg) 
{
  for (let i in arg)
  {
      arr.push(...arg[i]);
  }

  for (let i = 0; i < arr.length; i++)
  {
    for (let j = i+1; j < arr.length; j++)
    {
      if (arr[j] == arr[i])
      {
        arr.splice(j,1);
      }
    }
  }

  return arr;
}

#3

cool thanks :grin: i’ll keep this in mind and what u wrote is looking better than mine.


#4

You’re welcome! A good way to practice is to think that everyone that reads the code must understand what it does.