Map() method explain please

Map() method explain please
0

#1

Tell us what’s happening:
I am out of loop here. I miraculously solved the uniteUnique challenge on my own, and I din’t fully understand how my code works.:see_no_evil:
What i didnt understand: I understood how the code works in case of Test1,
but in case of Test2, what I dont get is, how the map() method handles the nested arrays.(since there is nested array in Test2. In my code, when I’m checking arr.indexOf(val), I’m checking the index of each elements in the array(for Test1), but in case of Test2, since it contains a nested array, how did the code handles this when the code encounters the array element as nested array? how did this work?

Your code so far


function uniteUnique(arr,...oth) {
  oth.map(item=>{
        return item.map(val=>{
            if(arr.indexOf(val)==-1)
            arr.push(val);
        });
     });
     return arr;
}

//uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);  //Test 1
uniteUnique([1, 3, 2], [1, [5]], [2, [4]]);        //Test 2

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sorted-union


#2

I’m thinking you may have discovered a flaw in the testing logic. .map() is not handling the nested arrays and neither is your test for equality.

.indexOf checks for equality using === which would not match on [5] === [5]

Clearly the test is not checking for a case such as
uniteUnique([1, 2, 3, [5]], [5, 2, 2, 2,[5]])
that would result in [ 1, 2, 3, [ 5 ], 5, [ 5 ] ] which would contain a duplicate.

Very good catch. Stay curious


#3

This would be a good candidate for a GitHub issue.


#4

When I console log the result, I got the same result as expected.That is,

console.log(uniteUnique([1, 3, 2], [1, [5]], [2, [4]])); //returns [1, 3, 2, [5], [4]]

So I’m confused, if there is flaw in the testing logic, wouldnt I get a different output in my console?


#5

You should get the same results regardless of where you run the code.

I really don’t think your code is flawed so much as I think the test cases are flawed (or are too difficult) and to make a function that would handle all possibilities including nested arrays is beyond the scope of the lesson.

When reading your initial question it occurred to me that a call like uniteUnique([1, 2, 3, [5]], [5, [5],[5],[5],[5]]) where there are duplicate subarrays would fail on solutions that the testing software would pass. Run that function call and you will see what the problem is - clearly not all duplicates are filtered out.

I’ve seen lots of solutions to this challenge I’m not sure that any of them adequately tested for that situation (mine did not). I only noticed because of your question about how the code checked nested arrays. I wouldn’t worry about it. It would be considerably more difficult to write a function that worked in that case and I don’t think that was considered when someone wrote the test cases.