Intermediate Algorithm Scripting - Sorted Union

Tell us what’s happening:
My code returns the correct solution, but the check says it is incorrect. I am not sure what is wrong…

**Your code so far**
function uniteunique() {
const a = {};
const nums = [];
for (let i = 0; i < arguments.length; i++) {
    arguments[i].map(y => {
        if (!a.hasOwnProperty(y)) {
            a[y] = 1;
            nums.push(y);
        }
    });
}
return nums;
}

console.log(uniteunique([1, 3, 2], [5, 2, 1, 4], [2, 1]));

**Your browser information:**

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Sorted Union

Link to the challenge:

This is not a correct way to use a map. A map is not a general purpose loop.

You changed the name of the function here.

Why is it poor practice to use map as a general purpose loop?

Two big reasons

  1. It is confusing for the reader. A map is only for making a new array out of an old array with const newArray = old Array.y(callback). Doing other things is like using the butt of a screwdriver to hammer in a nail.

  2. It is inefficient. Because map is for making a new array, an entire new array of the same length is allocated when you call map. If you never use the return value of the map, then you are allocating and throwing away that entire array.

Thanks. This isnt explained in the curriculum, but it is intuitive.

The map method iterates over each item in an array and returns a new array containing the results of calling the callback function on each element. It does this without mutating the original array.

Part of the lesson when map was introduced. It’s easy to forget.