Sorted union challenge.help

Tell us what’s happening:
In the below code, if the for loop condition is given as i<arr.length I get the first condition satisfied but not the second and third. then if i change the condition as i<arr.length-1 then the later two conditions are satisfied but not the first one. even after manually working the code i can’t seem to notice my mistake. Would appreciate if anyone can point out my mistake.Thanks in advance :slight_smile:

Your code so far


function uniteUnique(arr) {
var args = Array.prototype.slice.call(arguments);
arr = args.join().split(',').map(x=>+x);

var newArr = [];
for(var i=0; i<arr.length-1; i++){
newArr = arr.filter(function(item){
  
  return newArr.indexOf(item) == -1?newArr.push(item):0;
  
});
}
return newArr;
}

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/84.0.4147.89 Safari/537.36.

Challenge: Sorted Union

Link to the challenge:

check again if

return newArr;

is at the right place!

1 Like

Thank you . I figured it out. I had a completely unnecessary for loop over there. My code would have been just fine without than :sweat_smile:. Than you once again :slight_smile:

1 Like