function uniteUnique(testArray) {
arr=Array.prototype.slice.call(arguments);
var flat=flattenArray(arr);
console.log(flat);
allUnique(flat);
console.log(flat);
return flat;
}
function flattenArray(arr) {
//var flattened=[];
return arr.reduce(function(acc, item) {
//debugger;
if (Array.isArray(item)) {
// console.log(acc,item);
return acc.concat(flattenArray(item));
}
return acc.concat(item);
}, []);
}
function allUnique (arr) {
for (i=0; i< arr.length; i++) {
// for (j=0; j<testArr.length; j++) {
if (arr.indexOf(arr[i], i+1)>0) {
var leftSide=arr.indexOf(arr[i],i+1);
console.log(arr.indexOf(arr[i],i+1));
var fixedArr=arr.splice(leftSide,1) ;
console.log(arr);
// console.log(arr);
allUnique(arr);
}
}
}
//var test=[[1, 3, 2], [5, 2, 1, 4], [2, 1]];
uniteUnique([1,1]);
so with all that code above, I wanted to say that apparently my 'flattenArray function apparently goes too far in that it goes til there is no where else to go. so for this test:
uniteUnique([1, 3, 2], [1, [5]], [2, [4]]) should return [1, 3, 2, [5], [4]]
I get the following results:
[1,3,2,5,4]
which is correct except for all the extra brackets.
I’m actually not sure why my answer is incorrect so if someone could help me there, I’d appreciate it.
Secondly, I read above about testing arguments of less than two, and my solution above yields what certainly looks like correct results.