Sorted Union - experimenting with solution

Hi

I started this problem by wanting to unite all the elements from the arrays into one array and then play with that array to find the solution.

I tried to create this using a for look with the final parameter equal to (arguments.length - 1) because the function is using concat on (arr[i+1]). If I use i++ it would loop once too often

My console.log isn’t outputting a result so my code is wrong!

Is there a way to use a for loop like this where it keeps looping relative to the arguments length?

Your code so far


function uniteUnique(arr) {

let monsterArr = function(arr){
  for (var i = 0; i < arguments.length; i = arguments.length - 1){
    arr[i].concat(arr[i+1])
  }

  return monsterArr;
}



}

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


Your browser information:

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

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

Also tried this!

function uniteUnique(arr) {

let monsterArr = function(arr){
  for (var i = 0; i < arguments.length; i++){
    for (var j = 0; j < arguments[i].length, j++){
       return arguments[i][j];
    }
  }


}




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

Hi Randell

Thanks for the reply.

Does the first function not reply monsterArr from within the function?

Not sure what you mean by “reply”. You have declared a function inside another function but never call it so it never does anything. Why not just put the code you have in the inner function as the main function’s code instead of adding another function?

Thanks for your help and patience.

I think I have managed to put all the elements into one array using this code:

function uniteUnique(arr) {

let args = [...arguments];
let monsterArray = [];


  for (var i = 0; i < args.length; i++){
    for (var j = 0; j < args[i].length; j++){
       monsterArray.push(args[i][j]);
       
    }
  }
return monsterArray;

}