Can someone tell me how to solve this problem correctly using nested loop

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

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

  // Only change code above this line
  return newArr;

console.log(filteredArray([["trumpets", 2], ["flutes", 4], ["saxophones", 2]], 2) );```

      **Your browser information:**

User Agent is: <code>Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0</code>

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

**Link to the challenge:**

would you be able to explain what the challenge is asking?

We have defined a function, filteredArray , which takes arr , a nested array, and elem as arguments, and returns a new array. elem represents an element that may or may not be present on one or more of the arrays nested within arr . Modify the function, using a for loop, to return a filtered version of the passed array such that any array nested within arr containing elem has been removed.

and if you check the output, what’s wrong with it?

Output is [ [ ‘flutes’, 4 ], [ ‘saxophones’, 2 ] ]

It should be [ [ ‘flutes’, 4 ]} only

Actually I know whats wrong with my code and why but I want to how to fix it using the same method of nested loops

it’s not the nested loops that’s the issue - it’s changing the array you are iterating on. splice changes the array, so the loop is jumping over some items

1 Like

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