# Intermediate Algorithm Scripting - Sorted Union

The console.log of the array shows the code is working but is concluding with an error of some kind.
[ 1, 3, 2 ]
[ 1, 3, 2, 5 ]
[ 1, 3, 2, 5 ]
[ 1, 3, 2, 5 ]
[ 1, 3, 2, 5, 4 ]
[ 1, 3, 2, 5, 4 ]

I think it is being fidgety with arr[n].length. However, console log of arr[n].length also seems to think it is working with the right arrays.
4
4
4
4
2
2

``````  **Your code so far**
``````
``````function uniteUnique(...arr) {
for (let n=1; n <= arr.length; n++) {
for (let i=0; i<arr[n].length; i++){
console.log(arr[0]);
if (!(arr[n][i] in arr[0])){
arr[0].push(arr[n][i]);
}
}
}
return arr[0];
}

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/104.0.0.0 Safari/537.36`

Challenge: Intermediate Algorithm Scripting - Sorted Union

Why <= here?

I don’t think this line does what you think it does.

Also, modifying the contents of the first input array 1) isn’t always a great idea as it spills the effects of the function outside of the function and 2) your first array won’t always have strictly unique values.

O.K., I fixed those issues, but I seem to be getting other odd results. It is not resolving the last two tests, but my output seems to be illogical.

``````function uniteUnique(...arr) {
let result=[];
for (let n=0; n < arr.length; n++) {
for (let i=0; i<arr[n].length; i++){
console.log(result, arr[n][i]);
if (!(arr[n][i] in result)){
result.push(arr[n][i]);
}
}
}
return result;
}

uniteUnique([1, 3, 2, 3], [5, 2, 1, 4], [2, 1]);
``````

console.log(result, arr[n][i]); for the last two tests are as follows:

1
[ 1 ] 3
[ 1, 3 ] 2
[ 1, 3, 2 ] 5
[ 1, 3, 2, 5 ] 4
[ 1, 3, 2, 5, 4 ] 5
[ 1, 3, 2, 5, 4, 5 ] 6

1
[ 1 ] 3
[ 1, 3 ] 2
[ 1, 3, 2 ] 3
[ 1, 3, 2, 3 ] 5
[ 1, 3, 2, 3, 5 ] 2
[ 1, 3, 2, 3, 5 ] 1
[ 1, 3, 2, 3, 5 ] 4
[ 1, 3, 2, 3, 5 ] 2
[ 1, 3, 2, 3, 5 ] 1

I still don’t think this does what you think it does

Latest code

``````function uniteUnique(...arr) {
let result=[];
for (let n=0; n < arr.length; n++) {
for (let i=0; i<arr[n].length; i++){
console.log(result, arr[n][i],arr[n][i] in result);
if (!(arr[n][i] in result)){
result.push(arr[n][i]);
}
}
}
return result;
}

uniteUnique([1, 3, 2, 3], [5, 2, 1, 4], [2, 1]);
``````

I think that this line should generate an affirmative result when the current element is not already in the result array. The code below suggests that this is not exactly what is being resolved.

1 false
[ 1 ] 3 false
[ 1, 3 ] 2 false
[ 1, 3, 2 ] 3 false
[ 1, 3, 2, 3 ] 5 false
[ 1, 3, 2, 3, 5 ] 2 true
[ 1, 3, 2, 3, 5 ] 1 true
[ 1, 3, 2, 3, 5 ] 4 true
[ 1, 3, 2, 3, 5 ] 2 true
[ 1, 3, 2, 3, 5 ] 1 true

So two questions come to mind

1. how can you see what this line actually does?

2. is there an alternate way to do that task?

changing that line to if (!(result.includes(arr[n][i]))){ fixes the problem, but I don’t know why

Did you try 1)? When I want to know why a line is not working as expected, I try logging out pieces of the line to see what is happening.

I have run out of steam on 1). I posted a separate query on another thread about array.includes() vs. if…in syntax. I am waiting for a response.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.