I am stuck trying to create a function that combines the arrays provided as parameters into one union. Unfortunately, I keep encountering an error message:
JavaScript TypeError: Cannot read property 'length' of undefined
Here is my current approach:
function uniteUnique(arr) {
var newArr = arguments[0];
//Iterate through arguments
for (let i = 0; i<arr.length-1; i++){
console.log(arguments[i]);
//Iterate through values in the second argument
for (let y =0; y<arguments[i+1].length; y++){
//Iterate through values in the first argument
for (let z=0; z<arguments[i].length; z++){
if (arguments[i+1][y] !== arguments[i][z]){
newArr.push(arguments[i+1][y]);
}
}
}
}
return arr;
}
uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);
The desired output should be [1, 3, 2, 5 ,4], which represents a unique union of values from all three arrays given. However, I continue to encounter the same error.
TypeError: Cannot read property 'length' of undefined