For loop and indexOf

Hello ! I just passed this challenge but to be honest I don’t even know the meanig of all the codes here. Can someone please explain me step by step the codes below ? thanks

     for (let i = 0; i < arr.length; i++) {
if (arr[i].indexOf(elem) < 0) {
newArr.push(arr[i]);**
function filteredArray(arr, elem) {
let newArr = [];
// Only change code below this line
for (let i = 0; i < arr.length; i++) {
if (arr[i].indexOf(elem) < 0) {
newArr.push(arr[i]);
}

}

// Only change code above this line
return newArr;
}

console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));
  **Your browser information:**

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

Challenge: Iterate Through All an Array’s Items Using For Loops

Link to the challenge:

I’ll attempt to break this down one by one;

line 1: function filteredArray(arr, elem)
Here, 2 parameters are created. The use of arr within this data structure is for the individual array. elem is used as a parameter for selecting a number within the array to filter.

line 2: let newArr = [];
This is a new array of individual arrays that have been filtered and do not match the elem number later on within the data structure. Here it is just setting the value of the variable to an array.

line 3: for (let i = 0; i < arr.length; i++)
Basic for loop. arr.length is the amount of individual arrays within arr. In the challenge example above, there is 4 individual arrays within arr.

line 4: if (arr[i].indexOf(elem) < 0)
I would have done this differently. However, this is the filter part of the data structure. What this is saying is, any array within arr that contains the same number as elem will be eligible for this if statement. If an array does not include the number of elem, the array will not be added to newArr.

line 5: newArr.push(arr[i]);
The arrays that meet the above criteria will be pushed to the newArr variable, storing the arrays that meet the if statement within the newArr array. So if there were two arrays like [5, 10, 15], [10, 20, 30], the newArr value would now be [[5,10,15], [10,20,30]].

return newArr;
Ends the function execution and returns the value of newArr to the function caller.

console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));
This is using the arr and elem parameters of the filteredArray function.
First, the function is being declared. filteredArray();

Then, the arr parameter. filteredArray([arr]);

Since there are multiple arrays within the arr parameter, they will need to be declared as arrays. filteredArray([[number, number, number], [number, number, number]]);

Finally, the elem parameter is input at the end of the line of code. This is the value that attempts to match with a number within the arrays. If matched, the individual array will be added onto the newArr array through the for and if```` statement.filteredArray([[number, number, number], [number, number, number], number);```

Hope this helps. I only recently got passed this, so my explanation may not be the best. However, I recommend using MDN Documentation whenever you’re stuck or not understanding something. JavaScript | MDN

1 Like

Thank you very much. That was really helpful . Now I see better.

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