Iterate Through All an Array's Items

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

I am badly stuck here - tried to follow solution on other forum posts but cannot figure out how I can do this without using indexOf. Any help will be much appreciated please!

  **Your code so far**

function filteredArray(arr, elem) {
let newArr = [];
// Only change code below this line
for(let i = 0; i<arr.length;i++){
  let k = 0;
  let p = 0;
  for (let j = 0;j<arr[i].length;j++){
    if (arr[i][j] == elem){
      newArr = [];
      k++;
    }
    else {
      if (k>0){
        newArr=[];
    }
    else {
     p++;
        }
    }
  }
// Only change code above this line
return newArr;
}
}

console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 2, 9]], 3))
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36

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

Link to the challenge:

tried the following as well:

function filteredArray(arr, elem) {
  let newArr = [ ];
  // Only change code below this line
  for(let i = 0; i<arr.length;i++){
    let k = 0;
    for (let j = 0;j<arr[i].length;j++){
      if (arr[i][j] == elem){
        newArr = [];
        k++;
      }
      else {
        if (k>0){
          newArr=[];
      }
      else {
        newArr.push(arr[i]);
          }
      }
    }
  // Only change code above this line
  return newArr;
}
}

console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 2, 9]], 3))

this comes close

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

    for (let j = 0;j<arr[i].length;j++){
      if (arr[i][j] == elem){
        newArr = [];
        break;
      }
      else {
          newArr.push(arr[i])
      }
    }
  }
  // Only change code above this line
  return newArr;

}

console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 2, 9]], 3))```

Your getting close, but here you are resetting newArr to an empty array, which is not what you want to do.

You could in the first loop do something like:

let match = false

Then in the next loop if a match is found set match to true.

Then when the second loop completes check if match is true or false. If match is false push arr[i] to newArr.

Got it!!!

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

}

console.log(filteredArray([[3, 2, 3], [1, 6, 3], [2, 13, 26], [19, 2, 9]], 3))```

thanks a lot :slight_smile:

random text for 20 char

Being this is an educational forum, it is frowned upon to just give you an answer. That is why I offered a way to figure it out without just giving you the code to make it work. People learn better that way.

yes agreed! i would rather get a way then the solution!

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