Iterate Through Array's Items Using For Loops

Hello, I have some attempt at solving this question, but it doesn’t seem to work correctly.
In the inner loop if it encounters the element , the for loop should break. It breaks, but then it does still print the values in the inner loop that it shouldn’t after encountering pushArr = false;

function filteredArray(arr, elem) {
  let newArrs = [];
  let tempArrs=[];
  let pushArr = true;
  for(let i=0;i<arr.length;++i){
    for(let j=0;j<arr[i].length;++j) {
        if(arr[i][j] === elem) {
		  pushArr = false;
        }else  {
		  //create a temporary array for the inner loop array for i, this is needed as elem may be encounterd still in next loop
	//at the end of the for loop for i ,if the element wasn't encountered, pushArr wasn't set to false, so the temporary inner array should be pushed to newArrs.
	pushArr = true;
  return newArrs;
// change code here to test different cases:
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));

I hope someone can help. Thanks :slight_smile:

I think you’re killing yourself with complexity here. This is going to be tedious, but I’m going to suggest that you do a pen and paper walkthrough. What I mean by that is that you literally grab a pencil and go through every line of your code running an input and write down what every value is on every line. See when they change. A lot of people who are new to learning programming find this stupid, especially if they are self-teaching online, but it’s a genuine strategy for finding logical errors. In fact, it’s something you’ll see programmers do in job interviews when they whiteboard!

1 Like

don’t complicate things inside the for loops put simple if/else statement that checks if elem is present in the array, if yes then do nothing and if does not then push it into new array

Hello, thanks for the replies. Still not figured this out. Off course I could look at the hint/answer, but I’m trying to get a better understanding of this solving it my way. So , the idea is to only print the innerloop array element that don’t contain the element (elem).

I tried using every() or some() , still not luck. Now I get assert() is not defined error:

function filterArray(arr,elem) {
	let newArray = [];
	let shouldPush = true;
	for(let i=0;i<arr.length;++i){
        shouldPush = assert(arr[i].some(num=>(num!==elem))=== true);
	document.write("<br/>" + newArray);
} catch(ex) {
   document.write("<br/>" + ex);

Thanks so much for any help ! :slight_smile:

you are making it more complex than needed

step back from the code, pretend you don’t know any programming language
first, tell us what do you think should be the output, and also the smallest steps in your words that would bring from the input to the output
(share with as your thoughts about this algorithm)

1 Like

Figured out how to do this at last :slight_smile: using just some().
Thanks again.

you should look at the solution in the guide as a comparison
it may help you seeing that