Tell us what’s happening:
While I’m able to pass to test. I was frustrated and stuck for a long time about one problem. On the first for loop to put all arguments into one new Array. When I initially used k< arr.length, it does not work on all cases. Then I found out the arr.length shows a weird result.
For example:
uniteUnique([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8]) <-- arr.length is actually 3 but not 4. While arguments.length is 4 correctly.
uniteUnique([1, 2, 3], [5, 2, 1]) <-- again the arr.length is 3 not 2
I would like to know why arr.length is not equalivilant to arguments.length in all cases. Isn’t arr the function’s parameter to the arguments?
Your code so far
function uniteUnique(arr) {
let newArr = []
console.log(arr.length);
for (let k = 0; k < arguments.length; k++){
newArr.push(...arguments[k]);
console.log(newArr);
}
for (let i = 1; i < newArr.length; i++)
{
for (let y = 0; y < i; y++){
console.log(newArr[i] + " vs "+ newArr[y]);
console.log(newArr[i] === newArr[y]);
if (newArr[i] === newArr[y]){
newArr.splice(i , 1);
i--;
console.log(newArr);
console.log("deleted");
console.log();
}
else {
console.log(newArr);
console.log("next");
console.log();
}
}
}
return newArr;
}
console.log(uniteUnique([1, 2, 3], [5, 2, 1]));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sorted-union