Please help omg i've been doing this for sooo long :'(

My task is to .filter() every second .includes()

So therefore, shouldn’t my code achieve this by only resolving every second .includes(), as true?

When i call the console on this, it only logs the words that should be filtered

let j=1;

const howMany=storyWords.filter(wordy2=>{

if(overusedWords.includes(wordy2)){

j++;

if(j%2==0){

  return false;

} else {

  return true;

} else {
  return false;

}
}

});

Hi @highlandrocket295527
you have a problem with your scope: Both ‘else’ clauses are within the same scope which throws a syntax error. Your 2nd ‘else’ clause that returns ‘false’ is probably supposed to contain the condition for the case that overusedWords doesn’t include the current word. So you just have to move one closing curly bracket.
I guess you could have a simpler version by simply filtering the included words.

const howMany = storyWords.filter(wordy2 => overusedWords.includes(wordy2));

And then filter every other.

const everyOther = howMany.filter((_, index) => index%2 === 0)

I’m not sure if the code even then really does what you want it to do as it would filter every other word included independently from it being included only once, twice or triple etc. Therefore it might be more useful to filter out those words included more than once or twice?

PS: Please read this guide about how to format code in posts. That makes it easier for you and everyone else to read the code and spot errors.

It turned out i just confused the function of filter, it passes through on false statements, not true ones. lol

I confused the question though. I thought it wanted me to only swap every second occurrence but it meant per array element.

This is how i ended up resolving it:

let wordcount={};
const lol=overusedWords.forEach(word=>{
  wordcount[word]=0;
})

const howMany = storyWords.filter(word=>{
 if(word in wordcount){
    wordcount[word]++;
    if(wordcount[word]%2!==0){
        return true;
                    }else{
                          return false 
                         } 
 }else{
       return true;
      }  
});

Hmm, no. The filter accepts a callback function that is applied on each element. If it returns ‘true’ the current element is returned.

Does this do what you expect it to do? forEach always returns ‘undefined’, so ‘lol’ is undefined.

Yes it works if i log howMany

But what is ‘lol’ for?

To create an object, with key:value so that i can count the occurances of the words that im filtering out.

Ok, but you don’t need to assign it to ‘lol’. If you log ‘lol’ it’s undefined. You could simply write:

overusedWords.forEach(word=>{
  wordcount[word]=0;
})
1 Like

Indeed you’re right, i’m getting carried away with following the const = but it’s not needed here because the object is already defined, thank you!