Iterate Through All an Array's Items Using For Loops - Need help

Iterate Through All an Array's Items Using For Loops - Need help
0

#1

Tell us what’s happening:

The logic seems good so why is it not working. What am i missing?

Your code so far


function filteredArray(arr, elem) {
  let newArr = [];
  // change code below this line
  for (var i = 0; i < arr.length; i++) {

    for (var j = 0; j < arr[i].length; j++) {

    if (arr[i][j] !== elem) {
      newArr.push(arr[i]);
    }


  }

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

// change code here to test different cases:
console.log(filteredArray([[3, 2, 3], [1, 6, 4], [3, 13, 26], [19, 3, 9]], 3));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/iterate-through-all-an-arrays-items-using-for-loops


#2

Hey it looks like you are pushing subarray instead of values in each subarray. Look at your if statement for hints.


#3

Isn’t that what I have to do? I need to push the sub arrays in which the given element is not present. Am i wrong?


#4

The issue is that the only way a subarray won’t be pushed into newArr in your program is if every value in that subarray equals the element that’s being checked for. So each time the value you’re trying to filter isn’t found, it pushes the entire subarray. Log your newArr into the console to see exactly what’s going on.


#5

Ok, let me try something.


#6

Ok i need help. Kindly help.


#7

You don’t want to push a subarray everytime a value in subarray doesn’t match your elem. You need to push a subarray only after you check everything in the subarray that there is no match.

For example, look at this subarray[1, 6, 4] with element being 3. You are pushing this subarray without checking if this subarray contains 3 first. You are just pushing it because the first value which is 1 doesn’t match 3.


#8

But I am matching every value one by one right? not just the first value.
Secondly, how to i match an entire sub-array for a value?


#9
  1. Yes you are.
  2. Use

Or


#10

The includes method has not been covered in the lessons yet. And I don’t see how indexof() can be used here… im confused…Could you please explain what my code is doing?


#11

If i use the includes method I will only need one for loop. Am i right?


#12

Correct,

You can just use one loop and by using Array.includes, you can check if the sub array contains a value or not :slight_smile:

If the subarray includes it, dont push it but if it does, push it.

Give your last shot and try it.

Here is the solution.

Summary

function filteredArray(arr, elem) {
let newArr = [];
// change code below this line
for (var i = 0; i < arr.length; i++) {

if (!arr[i].includes(elem)) {
  newArr.push(arr[i]);
}

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


#13

But what if i do not want to use the includes method?


#14

Ok so i was able to solve it with this code:

function filteredArray(arr, elem) {
  let newArr = [];
  // change code below this line
  for (var i = 0; i < arr.length; i++) {


    if (arr[i].includes(elem) === false) {
      newArr.push(arr[i]);
    }


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

// change code here to test different cases:
console.log(filteredArray([[3, 2, 3], [1, 6, 4], [3, 13, 26], [19, 3, 9]], 3));

#15

But what if I couldn’t use includes? what then?


#16

Like I said above, you can also use method index of which essentially does the same thing.

Or you could run the double for loops like your original code and make a boolean flag before your 2nd loop runs. Only change the flag if it meets your condition and when the inner loop exits, if the boolean variable is changed then push the subarray.

Hope this helped out :slight_smile:


#17

Great. Thanks for helping me out.