Mini linter question


#1

I’m wondering something about step 4 in the Mini Linter challenge.

Step 4:
“There is an array of words called overusedWords. These are words overused in this story. You want to let the user of your program know how many times they have used these overused words. There are two ways to achieve this. Try it on your own first. If you need help, consult the hint.”

I achieved this successfully (see code below).

But I don’t really understand how this objective can be reached as suggested in the hints noted below. I’ve been trying to implement the hint ways to get a better understanding, but somehow I can’t get it working or I’m misunderstanding the hints… Any tips?

Hint 1: You can iterate over the betterWords array three separate times (once for each of the words in the overusedWords array). Create a variable that represents the total times that word appears. Add 1 to the variable every time the current word is the same as that word.

Hint 2: You can make this simpler by using one if, and two else if statements in the function code block of your iterator. That way, you can gather the counts of all three overused words at one time.

let story = 'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse, I began my trek across the George Washington Bridge into New Jersey.  The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side.  An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson.  Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.';

let overusedWords = ['really', 'very', 'basically'];

let unnecessaryWords = ['extremely', 'literally', 'actually' ];

// Personal note: Split the single string in the story array to a new array with separate items per word

let storyWords = story.split(' ');

// Personal note: Create new betterWords array and filter out the words defined in the unnecessaryWords array 

let betterWords = storyWords.filter(function(word) {
		if (unnecessaryWords.includes(word) === false) {
      return word;
    } 
});

// Personal note: Create new countOverusedWords array and filter out the words defined in the overusedWords array  

let countOverusedWords = storyWords.filter(function(word) {
  if (overusedWords.includes(word) === true) {
   return word;
  }
});

// Write to console

console.log('The number of overused words in this text is: ' + countOverusedWords.length);

#2

i think the problem with your implementation is that it has a big o notation of O(N2)

it would be more effective to have an O(N) algorithm implemented, you read about this here:

https://rob-bell.net/2009/06/a-beginners-guide-to-big-o-notation/

let me know if you think i got it wrong.

So what hint 1 is suggesting is to loop over overuserWords array, then within this loop nest another loop to loop over storyWords, so something like:

let count = 0
for overusedWord in overUserwords{
    for word in storyWords {
        if word equals overusedWord, increase count
   }
}

the above code is pseudo code, not actual code as you might have guessed.

but this also a O(N2), so instead is suggest to loop over storyWords, and then use an if statement to check if word equals overWords[0], and else if to check if word equals overWords[1] or overWords[2]


#3

Thanks for the input, I’ll have a look later today to see if I can manage to change the code.


#4

I’ve now updated my code so that there is only one function for both goals. Kinda feels that this is not really what you’ve suggested above (still hard to wrap my head around all these concepts), but I think the new code is at least a small step of improvement :slight_smile:

let sentenceCounter = 0;
let countOverusedWords = storyWords.filter( word => {
  if (overusedWords.includes(word) === true) {
    return word;
  } if (word[word.length-1] === '.' || word[word.length-1] === '!' ) {
    sentenceCounter++;
  }
});

#5

No? The point was not to use a for loop or if/else if instead of includes.


#6

I think I am getting confused by returning back halfway in the exercise a day later. I’ll go back to step 1 of the exercise and walk through all steps again to refresh my memory.


#7

The hints you struggle to understand is because you decide to use .includes(), where as the hints refer to using if/else if instead of includes or another for loop instead of includes


#8

Ah right! So that would be something like this? And of course that could be made more elegant without manually giving the index of the array etc… And this can probably be added to one single function.

let betterWords = storyWords.filter( word => {
		if (word != unnecessaryWords[0] || word != unnecessaryWords[1] || word != unnecessaryWords[2]) {
      return word;
    } 
});

and

let sentenceCounter = 0;
let countOverusedWords = storyWords.filter( word => {
  if (word === overusedWords[0] || word === overusedWords[1] || word === overusedWords[2]) {
    return word;
  } if (word[word.length-1] === '.' || word[word.length-1] === '!' ) {
    sentenceCounter++;
  }
});

#9
let betterWords = storyWords.filter( word => {
		if (word != unnecessaryWords[0] || word != unnecessaryWords[1] || word != unnecessaryWords[2]) {
      return word;
    } 
});

is fine, but i would do:

firstUnnecessaryWords = unnecessaryWords[0]
let betterWords = storyWords.filter( word => {
   // rest of code
}

if you really go for performance, to save array lookups.

For the rest, you seem to understand everything fine :slight_smile:


#10

Thanks for the tips and suggestions! Time for a coffee break after all this head spinning :sweat_smile: