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=[];
var arrayCount=0;
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, 3, 2], [1, [5]], [2, [4]]);
Apologies for the re-post. First I was trying to figure out how to post my code in a reply.
More importantly, for this challenge, the test:
uniteUnique([1, 3, 2], [1, [5]], [2, [4]]) should return [1, 3, 2, [5], [4]]
I get the following return:
[1,3,2,5,4]
which is correct except for extra brackets.
i use the function ‘flattenArray’ to get all arrays into one array and you can see from the code that it keeps going (flattenArray) until there are no more ‘sub’ arrays.
Can someone tell me why my answer is incorrect?
Thank you.