Remembering a filterd variable

Hi, been a user for a month first time asking for help so bear with me, please.

I have made a loop to filter parts of the argument but I have a problem that I can’t solve. In short, I don’t know how to return the filtered part of my code so it loops again and filters THE ALLREADY FILTERED CODE with the next argument in line. I know I can create a new variable and filter the filtered code but that really doesn’t solve anything since there can be x amount of arguments which i don’t know. There might be other ways to solve the problem (but I would really like to know how to continue looping the filtered variable) at hand and I hope someone can help.

Tnx in advance,
Cheers.


function destroyer(arr) {
for (let j =1;j<arguments.length;j++){
let a = arr.filter(value => !(value==niz[j]))

}
}

console.log(destroyer([1, 2, 3, 1, 2, 3], 2, 3));
//
**Your browser information:**

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

Challenge: Seek and Destroy

Link to the challenge:

Hi @zgodni ,

Try putting the for loop inside the filter method?

a is a new variable at each iteration of the loop

what you need to do is update the value of a variable at each iteration, storing the output of filter and using that value in the next iteration

how?

try to think how you were doing for example summing all numbers in an array with a loop, you need to do something similar with this more complex use case

Yea I have scoured the internet and my notes for the last 3 days, went thru thousands of tutorials because I have been stuck on this problem and I still can’t find what I’m looking for, maybe the problem cant be solved the way I want it. @manjupillai16 I’m sorry but I don’t know what you mean by that. @ILM I understand what you want me to do (simply similar to adding sum += i in a for loop) but I just don’t know how to do it. Tnx for your help anyway guess ill tackle the task from a different angle :v: cheers.

you want the output to be a, right?

then you can start with let a = arr outside the loop, and update the value of a inside the loop

Hey zgodni,
can understand, these methods can initially be very confusing. Will try to elaborate on my suggestion.

/* a is the filtered array without the elements present in arguments[1] and arguments[2] in the given example: */

let a = arr.filter (value => {
for (let j =1;j<arguments.length;j++){
//if value is equal to arguments at position j
return false;
}
}
return true;
});
//finally return output array a;
}

Hope this helps

Hi, the easiest way is to do as @ILM says, declare the array variable outside of your for loop, and update its values inside the for loop.
Pseudocode would look something like:

function {
a=copy of array to be filtered
for every other element in the arguments {
a=a.filter(filter condition)
}
}

1 Like

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