Mini Linter project question 8 step 3

Is it possible to get some help with the Mini Linter Project question 8 step 3? I am not sure how to go about it. This what I’ve tried so far

let replacedWords = overusedWords.filter(word => { if (word === 'really' || 'very' || 'bascically' return 'not really', 'not very', 'basically not' }); console.log(replacedWords);

https://www.codecademy.com/courses/introduction-to-javascript/projects/mini-linter

Thanks!
Scott

Hi Scott,
what exactly do you want to do? I’m currently not subscribed to PRO, so I can’t see what question 8 is asking you to do.
As you can see in the Codebyte, there is an issue with your return statement. You can only return one object. That can be one string, one array (with multiple strings or other values) etc.

Your filter callback needs to be a predicate function. None of that logic belongs in there.

Thanks.

step 3 of the question is asking to replace a 3 string array assigned to a let variable with 3 new strings using an iterator like .reduce, .foreach, .filter, .map,

I have now removed the IF statement I had. Not sure what needs to be done.

let replacedWords = overusedWords.filter(word => { return ['not really', 'not very', 'basically not'] }); console.log(replacedWords);

by logic you mean the IF statement?

Yes, and all the attendant code.

Array.filter(callback)

const callback = x => condition containing x

The return value of the callback function will be a boolean, which if true will allow the current value of x to be inserted into the array that .filter() returns.

Consider how Array.includes() might fit into this picture.

1 Like

Hmm, it’s a while ago that I did this exercise, but I cannot remember that replacing an array of three strings with three different strings was part of it.
I remember that there was an array with three strings. These strings should be filtered out of an array of story words entirely. And then there was another array of three strings with words used too often in the story. These should be counted and just a few of these words should be filtered out, but I don’t remember exactly.
The filter function for an array of three items iterates three times. In each iteration one item from the filtered array is assigned to the variable ‘words’ – or ‘x’ in @mtf 's example – and compared to a condition provided. Your function does not provide such a condition.

ok so… after much thinking I’m STILL confused!! haha.

i need a conditon in the filter function which must contain ‘x’ as @mtf mentioned correct? do I need to create that condition with an IF statement that would use .includes() on the overusedWords array then a return statement that has an array of 3 strings I want to use instead?

This is what I have so far…

const replacedWords = overusedWords.filter(function(replacedWords) { return overusedWords.includes('not really', 'not very', 'basically not') }); console.log(replacedWords);

Ok, I think we have to define two things:

  1. What do you want to do exactly? Why would you replace one array of 3 words with another array of 3 words with the filter function? Why wouldn’t you just reassign the variable? Are you sure that this is what you’re asked to do?
  2. How do the filter method and the includes method work? What values are assigned to the parameters – like ‘replacedWords’ – in the callback?

You named your variable ‘replacedWords’ – plural – which indicates that you don’t have a clear idea of what is assigned to that parameter/variable. What would you expect the value of ‘replacedWords’ to be and what will it be used for in the function body?

I would recommend that you split the task and write the callback function first:
A function with a parameter that takes 1 argument (1 word of the array) and returns true or false. True if you want that word to be in the new array – false if you don’t want the word to be included in the new array.
But at first I would recommend that you describe the task and for what purpose you’re supposed to do what. I doubt that you should just replace an array of 3 words with another array of 3 words.

@mtf named the parameter in his example ‘x’. I called it ‘word’. It doesn’t matter how you name it. It’s the same thing.

Add:

You don’t need and additional if statement. includes() returns true or false. But it accepts only one argument.

Thank you @mirja_t.

Here is a screen shot of the task to be completed.

You are correct in saying i have no clear idea… lol…

So you’re trying to do step 8.1, right?
If I remember correctly, you already split the long string assigned to the variable ‘story’ in one of the previous tasks and assigned it as an array of words to another variable (‘storyWords’), right?
Anyway, you should remove every second appearance of each of the three words assigned to overusedWords within the string ‘story’ or – in case you already did that – in the array ‘storyWords’.
That means that you don’t need to replace anything and it means that the array to be filtered is a lot longer than just three items.
This is one possible way of how you could approach this:

  1. You need an array with all the words in ‘story’. That’s what the .split() method can be used for
  2. Then you could write a callback function with one parameter that accepts one argument (a word from the ‘story’) and that has a counter for occurrances of a word included in the overusedWords array. This callback returns true if the argument passed to this callback function is not listed in the overusedWords array or if your counter is at 0 (because you only remove every other of the occurrances). You can use the .includes() method for that.
  3. Now you just need to apply the .filter() method to the ‘storyWords’ array and pass the callback function from the previous step to it.