[SOLVED]Intermediate Algorithm Scripting: Sorted Union indexOf === -1

The following challenge I figured out the following strategy:

Create an empty array, loop through the values and see if the value exists in the existing array. If not (i.e. the index does not exist (so equals -1), add it.

For some reason I can’t get this to work with my own code which seems to be a combination of the basic code solution and the alternative basic code solution.

As the if statement I use newArr.indexOf(args[i][j] === -1) instead of the one (
!result.includes(args[i][j])) in the alternative basic code solution. Why does this not account for duplicates and give me the all numbers?

Here is my code in VSC (I get console.log should be return in the solution but I just want to show you guys that it returns all numbers): http://prntscr.com/n7ikht

Here is the challenge for what it’s worth: https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sorted-union

Are you sure this is the right way of checking the index of args[i][j]?

No I am not. If you look at the hints here at the basic code solution:


You can see that they create a new variable for each individual i and j and use that variable to see if it’s <0. I don’t get though why my solution would not work, which is why I am posting here.

I meant the syntax…

Inside the parenthesis is args[i][j] === -1 which most likely is false, but it could also be true, doesn’t change.
So now you have newArr.indexOf(false) which will most likely give -1
-1 is a truthy value (like all numbers excluding 0), so your if statement always execute
It will not execute only if you have false (or true In the other case) at index 0 of newArr

1 Like

Thank you so much! It happens a lot to me that I miss something like that. Funny how one little ) in the right place makes all the difference here.


Edit: I guess this looks cleaner but for some reason the other one above worked too: