Broken? Iterate Through All an Array's Items Using For Loops

Everytime i change the filteredArray input to test the cases that are tested on the bottom left, i get the correct output. But i fail the test. Here is a picture showing in the console that i am getting the correct output. Am i missing something?

Your code so far


function filteredArray(arr, elem) {
  let newArr = [];
  // change code below this line
  loop1:
  for (let i = 0; i < arr.length; i++){
    loop2:
    for (let j = 0; j < arr[i].length; j++){
      if(arr[i][j] === elem){
        break loop2;
      }
      else if(arr[i][j] !== elem && arr[i].length === j + 1){
        newArr.push([arr[i]]);
      }
    }
  }
  // change code above this line
  //console.log(newArr);
  return newArr;
}

// change code here to test different cases:
console.log(filteredArray([[10, 8, 3], [14, 6, 23], [3, 18, 6]], 18));

Your browser information:

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

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

Your console is not showing the correct result. Look at the bottom right of that image: what it shows is an array in an array in an array: 3 levels deep.

newArr.push([arr[i]]): with this line, you are pushing an array containing an array into your main array.

Also, out of curiousity, why labels? Have you got some extremely old teaching material that you’ve been referring to?

I googled break points when i was still having trouble getting it to work and an example used labels. I just took the labels off and it broke out of the first for loop like it was supposed too.

Thanks for catching my error. It had been racking my head. I also see how I wasn’t comprehending the console output now.

I decided to just now look at the hint section. How do i improve and not make my code overly complicated like i just did? The hint shows smaller, faster, and less bloated code than mine.

So, with respect to labels, the MDN says:

Note: Labeled loops or blocks are very uncommon. Usually function calls can be used instead of loop jumps.

So one clean way to complete this challenge is to do just that - write a function that checks if a value is in an array (which is what your inner loop does), and use that function:

function arrayIncludes(arr, elem) {
  for (let i = 0; i < arr.length; i++){
    if(arr[i] === elem){
      return true;
    }
  }
  return false;
}

function filteredArray(arr, elem) {
  let newArr = [];

  for (let i = 0; i < arr.length; i++){
    if (!arrayIncludes(arr[i], elem)) {
      newArr.push(arr[i]);
    }
  }

  return newArr;
}

Noe that the function is checking for prescence, not omission - arrayIncludes rather than arrayDoesNotInclude (then saying if NOT arrayIncludes in the condition) - because:

Checking an array contains a value is quite a common operation though, and JS provides a function for that:

function filteredArray(arr, elem) {
  let newArr = [];

  for (let i = 0; i < arr.length; i++){
    if (!array[i].includes(elem)) {
      newArr.push(arr[i]);
    }
  }

  return newArr;
}

There are many other ways to do this. Using labels to control loops is one way, it’s just that labels are incredibly uncommon in JS, for good reason, as there is [afaik] never a need to use them. Programming using GOTOs/jump statements is not something that is recommended outside of quite specific things - the advice against that style of programming goes back at least 50 years.