Intermediate Algorithm Scripting - Sorted Union

Tell us what’s happening:

Describe your issue in detail here.

Your code so far

function uniteUnique(arr) {
  const rest = [...arguments];
  const result = [];
  for (let i = 0; i < rest.length; i++) {
  for (let j = 0; j < rest[i]; i++) {
    if (!result.includes(rest[i][j])) {
  result.push(rest[i][j]);
    }
  }
  }
  return result;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36

Challenge Information:

Intermediate Algorithm Scripting - Sorted Union
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sorted-union**what’s wrong**

Hi, did you have an error message or question about this problem?

What did you try?
What isn’t working properly?
Is there an error?
Did you check the console for messages?
Trying using console.log() to print your variables and see what’s happening.

TypeError: Cannot read properties of undefined (reading ‘length’)

I don’t get that error with your code. However the function is returning []

Add some console.log() in there to see what’s happening. Try this:

function uniteUnique(arr) {
  const rest = [...arguments];
  const result = [];
  for (let i = 0; i < rest.length; i++) {
    console.log(i)
    console.log(rest[i])
  for (let j = 0; j < rest[i]; i++) {
    console.log(j)
    if (!result.includes(rest[i][j])) {
  result.push(rest[i][j]);
    }
  }
  }
  return result;
}

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.