Iterate all elements using for loops

can someone explain this challenge to me?

Why do we do this: newArr.push(arr[i]);

I thought the challenge is asking us to remove the subarray that contains the element? if the if condition is saying the element is not found, why are we then pushing that element in the arr?

What is the point of the return newArr? Isnt that just an empty array? How do the values enter newArr if the element is present?

  **Your code so far**

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) == -1){
// 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/92.0.4515.107 Safari/537.36 Edg/92.0.902.55

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

Link to the challenge:

the values are added to newArr with push, but only those values that do not contain the element to filter

I am sorry, I don’t quite understand. Please could you break it down for me.
I think I get that if the element is not present in the arr then every other element of the arr is pushed to the newArr. I think that is correct?

But how do the values get passed to newArr for the return, or does that simply just pass an empty array?

instead of removing the elements we do not want the input array, we are creating a new array that contains only the elements we want to keep, these being only the elements that do not contain the unwanted value
so, newArr is not an empty array, after the loop it contains the elements we want to keep

1 Like

I think I get it… So for the return newArr part does that return an empty Array only? If the element is found?

it returns an empty array only if there is no element to keep

1 Like

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