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
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: