Mini Linter Question

Hi there, I just finished the Mini Linter Project from the Iterators section of Intro to JS.

So, because I looked at the hint and video, I know what the code is supposed to be for filtering the words from the story to disclude unnecessary words:

const betterWords = storyWords.filter(word=>{
    if(!unnecessaryWords.includes(word)){
      return word
    }
});

But it said you could do it multiple ways, and I don’t know why the following doesn’t work:

const betterWords = storyWords.filter(word=>{
  if(word!=='extremely'){
    return word
  } else if(word!=='literally'){
    return word
  } else if(word!=='actually'){
    return word
  }
});

This is probably a dumb question, but I would greatly appreciate any insights! Thank you!!

Hello, @nupham, and welcome to the forums!

Consider carefully the logic. What if word is extremely? Let’s follow it…

if(word !== 'extremely') { //word is 'extremely', so this is false
  return word //this is skipped
}

The first if condition is not met, so we move to the else if:

} else if(word !== 'literally') { //this is true, so the next line is executed
  return word //'extremely' is returned even though you had intended to filter it out
}

Now the problem is evident. Every word in storyWords will be returned. All words that aren’t ‘extremely’ get returned by the initial if condition. ‘extremely’ gets returned by the first else if condition. The final else if is never reached.

It is important to understand what return does. It always hands a value, and directs the control flow back to the ‘caller’ effectively terminated the encompassing function or method. We don’t want to return a value until we’ve completed the intended operations. Your method (using if...else if...else) for accomplishing the task can work, but needs a little tweaking. Keep in mind that you probably want to end an if...else if... with an else. If you find you don’t need an else, then you can just use a series of if's rather than if...else if...else if. Give it another shot, and feel free to ask additional questions.

Also, for future posts, to have your code retain its original formatting, and appear as it does now (I edited it for you), please follow the guidelines found here.

Thank you very much!!

1 Like

Hi all, I also have a question about this project. It relates to finding the number of sentences.

I’ve written the following code:
// 5. count the number of sentences
let counter = 0;
const countSentence = betterWords.filter(element =>{
if(element.indexOf(’.’) !== -1 || element.indexOf(’!’) !== -1){
return counter++;
}
});
console.log(‘This text has ’ + countSentence.length + ’ sentences’);

The result is 11 when it should be 12. Where am I going wrong?

Entire code can be found on Github: https://github.com/woutje74/Iterators/blob/master/minilinter.js

Thanks in advance for the feedback!

1 Like

Hi all, would very much like an answer to my question posted a couple of days ago. Much appreciated!