Stuck in Iterate Through All an Array's Items Using For Loops

Stuck in Iterate Through All an Array's Items Using For Loops
0

#1

Tell us what’s happening:
This code returns the array with the elem on it. What is the trick so I can return the opposite of this, the array without the elem.

Your code so far


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

// change code here to test different cases:
console.log(filteredArray([ ["amy", "beth", "sam"], ["dave", "sean", "peter"] ], "peter"));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 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


#2

You could try using “indexOf” as

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

If you want to do it using only “for loops” try the below code

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

#3

Okay. thanks for the solution. Never knew I could use indexOf() like this.


#4

You got a wrong logic for filtering as it expected from work.

The work needs you filter(not push) any element specified as filter element, but your logic is about add it.

You should also consider to skip the rest of the array element once you find the element should be skipped using break.

Result array pushing also should be done for each top level array index, not using inner-loop(more leaf-level).

Both code answer by @pawansingh972 are right, but the second one is more logical.

Keep going on great work. Happy coding.


#5

@NULL_dev Thanks for the hint.


#6

I also have a question regarding this code. The code FCC suggests as an answer is so different from mine that I did not want to just copy it but rather wanted to save my code. I will be glad to hear any suggestions on how this could be fixed:

function filteredArray(arr, elem) {
let newArr = [];
// 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].indexOf(elem)!=-1);{
newArr = arr.splice(arr[i],1);
}
break;
}

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


#7

Hi @Karajna
I don’t understand why you are looping inside the elements of arr[i]. You never even use your j variable.

Then you have a bad semicolon in this line, which you should remove: if (arr[i].indexOf(elem)!=-1);

Finally, you are using splice on an empty array newArr. I think it would be more logical to:

  1. Check if elem is NOT part of arr[i];
  2. If true, then push the array arr[i] into newArr.

Finally, I don’t like that, in the solution provided, FCC changed a line above the comment let newArr = [...arr];. The comment clearly states to input your code below the 3rd line. What kind of rebelious solution is that? :stuck_out_tongue:

Cheers and happy coding.


#8

Thank you so much, this makes a lot of sense. My abstract thinking really sucks, but oh well, coding is meant to develop it, after all.

Indeed, the FCC solution has stricken me as a very rebellious one! That is why I wanted to work it out myself.

This was a great piece of advice, I will try to apply it! :slight_smile: happy coding!