Mini Linter - answer validity

Hello!

I have recently completed the Mini Linter project and just had a question about the validity of my answers for steps 4 and 5.

const wordCount = betterWords.filter(word => {
  let count = 0
  if (word === overusedWords[0] || word === overusedWords[1] || word === overusedWords[2]) {
    count++
  }
  return count
})

console.log(wordCount.length)

const storyCharacters = story.split('')
const punc = storyCharacters.filter(word => {
  let count = 0
  if (word === '.' || word === '!') {
    count++
  }
  return count
})

console.log(punc.length)

I have been able to get the correct answer by logging the length of the variables I have created and answered the question by using iterators (I am sure there are multiple ways of answering the questions but this is what I could come up with at this time). However, I just wanted to get some insights as to if this is an appropriate way of answering the question, but also why am I not able to return the length of the count within the functions? If I log the variables (overusedWords and punc) by themselves, an empty array is returned.

My best guess is that it has something to do with local and global variables but I can not find answer that is satisfactory to me about why these behaviors are occurring and although I feel comfortable moving on, this is going to bug me for a while.

Any advice is appreciated!

Hi,
with the filter methods you want to filter the items of an array. That means you should return those items that meet your condition. But you return the count instead of the items.

The only thing is that when I return the count, it returns the array of elements that have been added to count that have met the condition even though I have set a variable of count = 0.

I also tried it this way:

const wordCount = betterWords.filter(word => {
  let count = []
  if (word === overusedWords[0] || word === overusedWords[1] || word === overusedWords[2]) {
    count.push(word)
  }
  return count.length
})

console.log(wordCount)

But it still requires console.log(wordCount.length).
I may be misunderstanding your answer.

Thank you for the response though.

Actually I think I get what you are saying. I’ll give it a shot and report back, thanks!

1 Like

Your filter should have a predicate function that determines which values get returned. It is the length of the returned array that we want, but only after the filter has been applied.

Recall that .filter is building the array so we don’t have a build procedure within it. All the lines in your code should be removed and a simple predicate inserted, instead.

Consider what other iterator will come in handy as the predicate.

Hey thanks for your response. I may have figured out a solution based on your advice. Any thoughts on this?

const wordCount = betterWords.filter(word => {
  return overusedWords.includes(word)
}).length

console.log(wordCount)

It gives me the same result as before. Also, just so I have it clear, the .length at the end of the function is to get the number of elements of the whole array that I returned from within the function itself correct?

1 Like

Correct. The return value of .filter() is an array, which has a length property.

Aside

Since there is now just the one expression in the function, we can simplify it to the concise body syntax…

betterWords.filter(word => overusedWords.includes(word)).length