# Why are we writing -1 here?

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

2 Likes

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 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

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

I hope this helps clear the -1 (indexof) conundrum.

Imagine you have a big box full of smaller boxes, and each smaller box has some toys inside. We want to find and remove any smaller box that has a particular toy we donâ€™t like.

To do this, we need to go through each smaller box and check if it has the toy we donâ€™t like. If we find the toy, we will remove that smaller box from the big box. If we donâ€™t find the toy, we will keep the smaller box in the big box.

In our code, the `indexOf` method is like a way to find the toy inside each smaller box. When we check `indexOf`, it tells us the position of the toy in the smaller box. If it doesnâ€™t find the toy, it tells us `-1` instead.

So, if `indexOf` gives us `-1`, it means the toy is not in that smaller box. Thatâ€™s why we use `-1` in our code to check if the toy is not there. If we find `-1`, it tells us to keep that smaller box in the big box because it doesnâ€™t have the toy we donâ€™t like.

By doing this for each smaller box, we can remove all the smaller boxes that have the toy we donâ€™t like and keep only the ones that donâ€™t have it.

5 Likes

great answer thank you

1 Like

The best explanation i have read.Thank you!

Unless you need the index, I would suggest using Array.prototype.includes(), it is a cleaner API for this type of check.

Iâ€™m not even sure if or where it is taught in the curriculum.

``````if (!arr[i].includes(elem)) {

}
``````

Example:

``````function findName(nameList, name) {
return nameList.includes(name);
}

console.log(findName(["John", "Jane", "Joe", "Jack"], "Jane")); // true
console.log(findName(["John", "Jane", "Joe", "Jack"], "Bob")); // false

``````

There is not better way to explain it. Appreciate it!

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