# Help me with "Mini Linter" project item 8.1

#1

Hi guys!
I’ve tried to look out for a past discussion about step 1, item 8 in the “Mini Linter” project but couldn’t find any.
I’m talking about - "For the overused words, remove it every other time it appears."

Let’s cut to the chase… I’m pretty lost…

P.S - Is there anybody else here who thinks the Arrays section is the hardest in the “Learn Javascript” course?

Thanks for those of you who read this far.
Daniez

#2

Look back over the tools you have accumulated, thus far, and at the name (and subject) of the unit… Iterators (link needed, please). These are built in tools for iterating iterables such as, strings, arrays and objects.

However, rather than throw ourselves headlong at the supposed solution (many will think it’s no trick) let’s think about the givens.

1. There are overused words present in the story text;
2. some of them appear more than once (or twice).
3. every other one implies toggling on and off;
4. etc.

Consider:

`````` > a = 0
<- 0
> a = + ! a
<- 1
> a = + ! a
<- 0
``````

This brings to mind a simple array,

``````["", overUsedWord]
``````

Starting with `a = 0`, on each encounter of the word, toggle `a` then replace the word with `array[a]`. In JavaScript we can do this in a single step.

Eg.
`````` > a = 0
<- 0
> list = ['word a', 'word b']
<- > (2) ["word a", "word b"]
> list[a = + ! a]
<- "word b"
> list[a = + ! a]
<- "word a"
> list[a = + ! a]
<- "word b"
> list[a = + ! a]
<- "word a"
``````

You get the picture. We now have a toggling mechanism which may (or may not) prove useful in our solution.

Toggle Explained

It’s important to note that it does not matter how we initialize our control variable, `a`. What it boils down to is truthy or falsy once the NOT (`!`) operator comes into play. NOT falsy is, true; NOT truthy is, false. We could set `a` to literally anything, the NOT operator will coerce it to a boolean. For our purposes we set it to zero to match our parameters (not the first, but the second, etc.).

``````! 'string'        =>  false

! function(){}    =>  false

! ''              =>  true
``````

This is reassuring since it gives us a guaranteed boolean, no guessing or special conditionals needed. Knowing that, we can be confident in the unary operator which is a coercive tool for changing a string (if possible) or a boolean to a number.

``````+ true            =>  1
+ false           =>  0
``````

For our toggle implementation we have explicitly geared our code to `0` and `1`. As seen above, the stage is wide open. The code dials itself down instantly.

``````+ ! 'five'        =>  0

+ ! []            =>  0

+ ! new Date()    =>  0
``````

Now we need to crawl through the text, word by word, and search for the target word, leave the first (replace it with itself) and remove the second (replace it with `''` or `null`), leave the third, remove the fourth, and so on.

How do we locate the word in the list? If this hasn’t been presented yet in the course then it may not be the expected solution, but there is no time like the present to introduce,

`Array.prototype.indexOf()`

Eg.
``````text = "The quick brown fox jumps over the lazy dog. The quick brown fox jumps \
over the lazy dog. The quick brown fox jumps over the lazy dog.".split(' ');

i = -1
i = text.indexOf('the', ++i)    // 6
i = text.indexOf('the', ++i)    // 15
i = text.indexOf('the', ++i)    // 24
i = text.indexOf('the', ++i)    // -1
``````

We now have the index of all the target instances (-1 means full length of array has been crawled).

prefix vs. postfix

When we prefix the incrementation operator the operaton happens first. What will happen when we postfix it? `text[-1]` is undefined, so, nothing. There is no point at which to begin indexing.

By prefixing the increment operator we jump `-1` to `0` which is a recognized index, and valid starting value for the method. We are able to loop through the entire sample because we always increment the last encounter position and make that our new starting point in the renewed search.

Another tool that may or may not yet have been introduced is,

`Array.prototype.splice()`

Eg.
`````` > text.splice(15, 1, '***')
<- > ['the']
> text.join(' ')
<- "The quick brown fox jumps over the lazy dog. The quick brown fox jumps \
over *** lazy dog. The quick brown fox jumps over the lazy dog."
``````

Even while we may be off the beaten path, we do now have all the tools we need to complete this task. Thankfully, in the programming kitchen there is more than one way to boil an egg, so when you have this method firgured out, keep digging for other approaches.

Proof of Concept

We didn’t bring up the two iterators you have just learned about, `Array.prototype.forEach()` and `Array.prototype.filter()` but they are covered for sure, at this point, and can be found documented on the MDN site we’ve already linked to above. Pretty sure we have already learned about `Array.prototype.join()`, and by this stage `do {} while ()`. That sums up all the methods and constructs this solution will implement.

``````text = "The quick brown fox jumps over the lazy dog. The quick brown fox jumps \
over the lazy dog. The quick brown fox jumps over the lazy dog.".split(' ');

const overUsedWords = ['the', 'quick'];

overUsedWords.forEach(function (x) {
let i = -1, a = 0;
do {
i = text.indexOf(x, ++i);
if (i > -1) {
text.splice(i, 1, [null, x][a = + ! a]);
}
} while (i > -1);
});
text = text.filter(x => x !== null).join(' ');
/* <-
"The quick brown fox jumps over the lazy dog. The brown fox jumps \
over lazy dog. The quick brown fox jumps over the lazy dog."
*/
``````

I’m dubious as to whether this is the expected solution for this problem as it should only implement things we know, from a purist perspective. Take what you might learn from this example but do continue to pursue other solutions. This is not the only egg in the basket.

Try it… overUsedWords filter

#3

I have to be honest, I didn’t quite understand this… (feel little embarrassed actually).
I don’t think I’ve encountered such syntax yet.
(Is this the place to mention I’m new to programming, and “Learn JavaScript” is the first course i’ve taken?) Everything so far was perfectly understood (I even found it quite easy), but the iterators part, oh man…

Sorry again, and thanks!
Daniel

#4

Click on `Toggle Explained` for a deeper explanation. We can cover it some more later, after you have had a chance to look into other possible solutions. As mentioned, this is probably not what is expected, though it works like a charm.

If you have the time, stay with this unit for a while longer before moving forward. Cover the lessons again, and explore each one in depth, including online reading of documentation and other examples that can be found on the subject. Don’t give up or push this aside. It is of vital importance moving forward.

The concept of iterators is not confined to JavaScript, but is a central facet of most languages. Grasp it in one and it will be easy to grasp in the others. Stick with it.