Sorted Union - it works, only it doesn't

Ok, near as I can tell from the console.log, this approach should work just fine. Running the arr argument through.toString().split(",") creates a string, then splits the string into an array (tested using console.log(Array.isArray(strArray)), which returns true.

Then, cycle through the elements in strArray, checking to see if a new array, newArr, includes the value. If not, push it. Everything looks 5x5, but the test still fails. Console.logs all check out, but the test still fails.


function uniteUnique(arr) {
 
  var strArr = arr.toString().split(",");
  console.log(strArr);
  var newArr = [];
  for(var i = 0; i < strArr.length; i++) {
    var currentValue = strArr[i];
    if(newArr.includes(currentValue)) {
      } else {
      newArr.push(currentValue);
      }
  }
  console.log(arr); // returns 1,3,2,5,2,1,4,2,1
  console.log(newArr); // returns 1,3,2,5,4 <-- the correct answer!
  console.log(Array.isArray(strArr)); // returns TRUE
  console.log(Array.isArray(newArr)); // returns TRUE

  return newArr;
  
}

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

Console:

1,3,2,5,2,1,4,2,1
1,3,2,5,4
true
true

Your browser information:

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

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

You are returning an array of strings instead of an array of numbers.
[ '1', '3', '2', '5', '4' ]

2 Likes

Awesomeā€¦ no idea how to fix that off the top of my head, but definitely lets me know where I need to poke. Thank you!!

Google. Search for ā€œfrom string to numberā€. VoilĆ :
Or donā€™t change to array to a string at the beginning, and find an other way so you have less steps

1 Like

You could go in a lot different directions with this one.

If you flatten your parameter array without converting to strings you avoid converting back to numbers. You might look at Array.concat(). concat takes multiple array arguments so spread operator could come in handy too.

You could also totally skip flattening and loop through all elements with nested loop pushing only unique elements.

Using parseInt() as you push onto the resulting array would fix your existing solution with only minor modification.

I definitely see the value of trying some other methods, but I really want to understand the ins and outs of what I attempted. Following the advice of using parseInt() to convert the string values back into numbers, I modified the definition of currentValue to

var currentValue = parseInt(strArr[i]);

and added this line below that, for testing:

console.log(currentValue, typeof currentValue)

The console.log output for each value of currentValue when it comes up for comparison is 1, number; 3, number; 2, number, etcā€¦which implies that now numbers are being added to newArr, but the test still fails.

Per @alhazen1ā€™s advice, I also tried modifying my push line to:

newArr.push(parseInt(currentValue));

The test still fails. Obviously, at this point, it probably makes more sense to go back to the drawing board with either Array.concat or nested loops using the arguments functionalityā€¦ but I need to know why this doesnā€™t work, first. Especially since it seemed like parseInt() should have been an easy-enough fix, even if the overall approach is non-ideal. Thanks!

Remember that array.includes() is looking for an exact match so for instance ā€˜3ā€™ does not equal 3.

Another option would be to use parseInt here with map to convert the string array back to numbers array.
var strArr = arr.toString().split(",")

Thanks. I donā€™t believe itā€™s the array.includes() thatā€™s failing: Changing the variable declaration means the test is comparing the integer version in both places. Alternatively, I also tested leaving the variable as is, and using parseInt() on both sides of the comparison and on the .push command. In all cases, the newArr ends up being filled with numbersā€¦

About parseInt(), you are missing a parameter in there. See the description of the method in the documentation

Thanks for the help @ilenia , but that didnā€™t seem to help. First, the radix parameter is optional, and without it, parseInt() defaults (correctly) to base 10 anyways. I tried specifying that, just in case, and it still doesnā€™t work.

I did notice that my way completely gets rid of the nested arrays, whereas the test cases show the returned array still having some nestingā€¦

uniteUnique([1, 3, 2], [1, [5]], [2, [4]]) should return [1, 3, 2, [5], [4]].

So, its very possible that even if I can get it working for at least the first test, it still might fail the rest. I just want to know why its not working for the first testā€¦ before I give up and rewrite from the ground up.

(I did just complete the exercise using the array arguments functionality and nested for loops. I feel no joy at this successā€¦ sigh)