No idea why I would push() arr[i] when newArr does not show the elem value in console.log

Tell us what’s happening:
I have no idea why I would push() arr[i] when newArr does not show the elem value in console.log
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) {
    //Checks every parameter for the element and if is NOT there continues the code
    //-1 means its not true, because in an index, -1 means an index thats not within an array
    newArr.push(arr[i]); //WHY IS THIS A PUSH WHEN NOTHING IS PUSHED INTO newArr????????
// Only change code above this line
return newArr;

console.log(filteredArray([["amy", "beth", "sam"], ["dave", "sean", "peter"]], "peter"));
/*based on the push() method, shouldn't the console log ["dave", "sean", "peter", "peter"]?
or if the elem does not exist (ie arr[i].indexOf(elem) == -1) and the elem is "joe for instance, shouldn't the console log [[["amy", "beth", "sam"], ["dave", "sean", "peter"], ["joe]]] because the push() method  is pushing whatever elem I put in into the array?*/

  **Your browser information:**

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

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

Link to the challenge:

arr has value of [["amy", "beth", "sam"], ["dave", "sean", "peter"]]
elem has value of "peter"
newArr has value of []

the loop starts

i has value of 0
arr[i] has value of ["amy", "beth", "sam"]
arr[i] does not contain "peter" (arr[i].indexOf(elem) == -1 is true) so it is pushed to newArr
newArr now has value of [["amy", "beth", "sam"]]

i has value of 1
arr[i] has value of ["dave", "sean", "peter"]
arr[i] includes elem so nothing happen

the loop ends

newArr is returned

the function otuputs [["amy", "beth", "sam"]]

1 Like

In the first argument = arr, get an array with elements in it, in the second argument passed into the function = elem, you get just a single string in this case. Your job is to “rewrite” the array method function, very similar to Array.filter.

When you check if the element with index i is the same as the second argument elem, you basically want to be sure that it is NOT the element that we want to erase from the array. If it is not, then the current element is pushed into a newArr, when is is this element, what mean that the condition won’t be met, code between if statement won’t be executed. In other words that exact element won’t be pushed to the newArr.

At the end of the function, you return newArr array as the answer, which in this case should look like ["amy", "beth", "sam"]

Thank you. I understand now that the for loop was iterating with arr[i] and if the array does not have the elem, it is pushed to newArr and into console.log(). But arr is a nested array (with 2 subarrays), so wouldn’t I have to iterate through arr, and then nest another for loop such as:

for (let i = 0; i < arr.length; i++) {
for (let j = 1; j < arr[i].length; j++) {
if (arr[i][j].indexOf(elem == -1) {
return newArr;

indexOf is checking the subarray, you don’t need a loop to do that

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