Why are we writing -1 here?

Continuing the discussion from freeCodeCamp Challenge Guide: Iterate Through All an Array's Items Using For Loops:

1 Like

indexOf function will return -1 if it is not finding a match
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

2 Likes

thank u :pray: this is good solve

1 Like

Cause indexOf return -1 when didn’t find the research. Also, i found another way to resolve the question.

Mod edit: solution redacted

Let me explain the if.

The if block of code checks if each element of an array of arrays does not contain the elem element passed to the function. If the element is not present, the index of the array is added to a new array. This is done using the includes() method, which checks if an array contains a specific element. If the elem element is present, the index is not added to the new array.

1 Like

thank you @filipehps
Solving the question in 3 languages (1-English, 2-Portuguese, 3-JavaScript) is quite colorful :slight_smile:

1 Like

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

2 Likes

that was my first post, sorry. thanks for the tip

2 Likes

That is basically if statement to check whether element is absent in array

1 Like