I just wanted to lay out my approach in the beginning. It seems like a pretty straight-forward challenge, but…I don’t know.
Your code so far
function uniteUnique(arr) {
//function taking 2 or more arrays returns one array of unique values in order of provided arrays
//1.Use 2-dimensional loop
//2.Return statement compares different iterations to determine if element has already appeared.
//3.If it has not, then it is pushed into new array.
return arr;
}
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/71.0.3578.98 Safari/537.36.
Your pseudo-code looks like it will work in terms of looping through every element of the array input and keeping the original order of the unique elements in a new array.
//Comments and concerns below the code.
function uniteUnique(arr) {
//Create empty container
var newArr = [];
//Arrange all possible values into single array
var args = [...arguments];
var len = args.length
//Use filter method to sift out unique elements
args.filter((val)=>{
for(var i = 0; i < len; i++){
//This is what I'm using to determine unique elements
if(val[i] != val[i]){
newArr.push(val[i])
}
}
//This shows nothing on my console
console.log(newArr)
})
//I don't pass anything
return newArr;
}
uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);
/*On a superficial level, it seems to me it should work. I'll state why I believe this below (1). Then, below that some doubts I have about this naivete(2). I would like holes blown in my logic wherever necessary...
//1.
What this challenge asks for is a search for non-duplicates. Anything that doesn't appear more than once is accepted and there for funneled into the empty vessel. I thought filter method would be perfect for this as it would return push those values that only appeared once into the empty container.
//2.
a. I had doubts that filter() would work as filter() is primarily a boolean method, correct? It only determines if something is true or false. I did try map() as well but nothing happened there is well.
b. I think that my method for determining on-duplicates is fraught with error. val[i] !== val[i] seems to repeat itself as val is, in fact, i, correct?
c. I am concerned that using [...arguments] is interfering with the process. I'm not sure why I feel this way, but perhaps adjusting the character of the different subarrays affects how the console is receiving my answer. (In terms of, this console is built toward receiving an answer completed in a certain way meant to teach some aspect of Javascript?)
*/
Just a question… do you want to have a future career in programming?
This is absolutely not what you are doing. You are not using filter. It is just there as decoration.
Refer to this please… (if you don’t want to return Anything inside the method, use forEach, each method has its applications, if you’re unsure, the documentation is there for you)
This is false, something is not different from itself. Obviously nothing is pushed.
Well, console.log the hell out of everything else… each variable for example, after its declaration, after it is changed in some way. Before an if statement console.log() the comparison you put inside the if statement
We can point out if you use the wrong method and such, but be a master of console.log() otherwise you will never know if your code does what you think it should do
So that was an incredibly flawed approach.
I seem to constantly misunderstand filter() so I’m REALLY going to have to sit down with this method again and again.
Anyway, my idea, given your comments, is that the best approach would be to use a basic for loop and push the first instance of every element into an empty container. Then, to return said container.
That seems pretty solid to me. I’m working on the code right now but the basic idea…that’s good, right?
Could you please wrap it in the spoiler tags, so if someone want the solution they can read it, if someone is just searching for hints they will not have the solution jump them on surprise?
Good. Just to reiterate what Leah’s saying is consistent with the intent of the forum. But you seem to get that now, so cool. I’ve added spoiler tags to your answer.