Intermediate Algorithm Scripting - Sorted Union

Tell us what’s happening:
Describe your issue in detail here.

I am not sure why this code is not working. I console-logged the answers and it’s giving me back the answers I’m looking for in the console, but none of the tests are passing. The same happened when I used “arguments” and the same happened when I filtered through my array at the end. They all showed the correct answer in the console, but somehow the tests aren’t agreeing with it…

Your code so far

function uniteUnique(...arr) {// use spread operator to create argument list

 // join arr into a string 
let newArr = arr.join(',');

  // split the arguments that were joined back into an array
  let args = newArr.split(',');

  // create a holding variable initialized to an empty array
  let final = [];

  // loop through the arguments and if the indexof the current index of args is equal to the index, push that index into the final array
  for (let i = 0; i < args.length; i++) {
    if (args.indexOf(args[i]) === i) {
      final.push(args[i]);
    };
  }
  
  
// return final array
  return final;
}

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

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

console.log(uniteUnique([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8]));

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

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/112.0

Challenge: Intermediate Algorithm Scripting - Sorted Union

Link to the challenge:

it looks like elements in your resulting arrays are strings, should be numbers

1 Like

You turned all the data into strings but the tests require numbers.

Ohhh, so I just need to convert them…didn’t realize that, thanks!

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