# Sorted Union Confusion

Having a problem with my solution that I do not understand.

``````function uniteUnique(arr) {
let newArr = [];

for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
if (newArr.indexOf(arr[i][j]) === -1) {
newArr.push(arr[i][j]);
}
}
}

return newArr;
}
``````

The problem I believe is in the second for loop specifically with the word usage of arr because if I use another word like arguments…

``````unction uniteUnique() {
let newArr = [];

for (let i = 0; i < arguments.length; i++) {
for (let j = 0; j < arguments[i].length; j++) {
if (newArr.indexOf(arguments[i][j]) === -1) {
newArr.push(arguments[i][j]);
}
}
}

return newArr;
}
``````

The word usage of arguments lets me pass the test and yet using arr which is already a parameter in the function returns an empty array. Any ideas as to why this is?

I will not give you solution, but I can tell the how to solve the problem, debug your code, as that simple. You can solve by easily and aware that how tackle the problem.

Hi there, thank you for your reply. So after thinking about it some more I decided to punch it into google which I could have done first but then I probably wouldn’t have posted it up here for others to learn from as well. I found the solution but I don’t want to give it away here. However, the solution as lead me to more questions and doesn’t really let me understand why the bug happened in the first place! It is tricky to explain without giving the answer away, but I will try my best.

When passing in the arr as a parameter to the function there is a certain operator we can use with the arr parameter that will expand the array. This solved it for me, but at the same time I really didn’t find out what was going on in my original solution, what was bugging my code! I ran it through my browsers debugger and the result the debugger returned was undefined, but what is undefined? Why is the console printing an empty array? I can see that my first loop (i) is iterating through the arrays in the array which would mean index 0, 1, 2 while the second loop (j) is iterating through the elements inside each of the array so [0[, , ,]] the if statement is saying that if the indexOf those elements doesn’t match with each other then it doesn’t get pushed into the empty array. Hmm is the code only pushing nothing? Do I need a second if statement to tell the console what to do with the matching numbers as it seems that their is no direction given to the code what to do with matching numbers or am I completely off in my logic?

The initial function is defined using a single parameter but the requirements state it will be passed two or more arrays. So it must accept two or more arguments. You can also see this in the tests.

`arr` is just a single parameter, it can only hold a single value. If you call the function with more arguments the rest of the arguments are lost as they have nowhere to be stored.

You either need an equal amount of parameters as you have arguments when the function is called or you need a “special parameter” that can contain as many arguments as needed.

The arguments object is an array-like object inside (non-arrow) functions that contains all the arguments the function was called with. In modern JS you will use rest parameters instead `(...args)`.

``````function logAllArgs() {
[...arguments].forEach((arg) => console.log(arg));
}

function logAllArgs(...args) {
args.forEach((arg) => console.log(arg));
}

logAllArgs(1, 2, 3, 4, 5, 6);
``````

You should not have gotten this far in the curriculum without understanding arguments and parameters. So maybe you just misunderstood the requirements and didn’t look at the tests.

1 Like

After googling I did find the rest parameter I was just trying to be discreet about it. I am not going to lie there are some things in this course which I passed by not fully understanding what was going on. I think I need to go back and look at some stuff again so that I can fully comprehend things before I move on.

Thanks for your reply though i appreciate you taking the time to help me understand.

Just keep asking questions on the forum to get help with the challenges. It is perfectly fine to not understand everything, but you have to ask about it and not just copy the solutions you find.

A lot of people seem to have problems with parameters and arguments.

I’m not 100% sure why. We have talked about it a few times and I have speculated that the tests calling the functions for people might be part of the issue. You need to understand the definition of a function to call it properly and you need to be able to look at the definition and call site (function invocation) and make sense of both.

As long as you write your own code you should be forced to use and think about parameters and arguments often enough throughout the curriculum that at this point they hopefully are not that difficult to understand.

1 Like

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