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

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! :hugs:

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!