# Mini Linter (Step 4 & 8)

For the “Mini Linter” project, I was very proud that I was able to come up with my own solution to Step 4 that didn’t need to be iterated three separate times and which only involved a single “if” statement.

``````// No triple iterating or triple if!
let overusedWordsCount = 0;
betterWords.forEach(word => {
if (overusedWords.includes(word)) {
overusedWordsCount++;
}
});
``````

Is there any way I could improve upon this further?

As for Step 8, here are my solutions for Task 1 and Task 3, respectively:

``````// Remove every other overused word
let moduloOverusedWordsCount = 0;
let moduloBetterWords = betterWords.filter(word => {
if (overusedWords.includes(word)) {
moduloOverusedWordsCount++;
if (moduloOverusedWordsCount % 2 === 0) {

} else {
return word;
}
} else {
return word;
}
});
``````
``````// Replace overused words
let evenBetterWords = betterWords.map(word => {
if (overusedWords.includes(word)) {
return 'especially';
} else {
return word;
}
});
``````

Can I improve upon these?

Finally, I am having the most troubles with Task 2. This is my best shot so far, but I honestly have no idea where to go from here:

``````// Find greatest word
function countWords(storyWords) {
return storyWords.reduce(function(countMap, word) {
countMap[word] = ++countMap[word] || 1

return countMap;
},{})
};
``````
3 Likes

who says `.includes()` doesn’t use a loop under the hood? This problem can be solved with O(N2) at best so far i know.

we can just use `.length`, given the array elements are the words. Why such a complicated solution?

here:

``````if (moduloOverusedWordsCount % 2 === 0)
``````

i would use does not equal or equals one. That saves you a `else` clause.

Hey stetim94,

who says .includes() doesn’t use a loop under the hood? This problem can be solved with O(N2) at best so far i know.

I only meant that I didn’t have to do it the way the instructions told me I did. But thanks for that knowledge. I didn’t know that.

we can just use .length, given the array elements are the words. Why such a complicated solution?

Honestly, I have no idea what I’m doing with that one. I’m not even sure what you mean by “use .length”. How?

i would use does not equal or equals one. That saves you a else clause.

That’s just genius! Thanks!

i am not entirely sure how `.includes()` is implemented, but it must a loop or something similar under the hood.

the other option is to use if condition and the or (`||`) operator.

`.length` is a property belonging to array, it gives you the length (number of elements) in the array. Given the elements in array are words within this story, we get the number of words within the story. You must have learned `.length`?

thank you. If you want to do nothing (which generally means you need to recheck your condition), javascript does have a `pass` keyword, take a wild guess what it does (sarcasm)

Ah, you may be unclear on the problem I’m trying to solve, regarding the .length thing. I’m trying to find the word which occurs the greatest number of times. Calling .length on the string array in question would return the number of elements in the array, but it won’t help me to store and compare the strings to find which one occurs the most.

Also, would using an if condition and ‘or’ operator improve the efficiency of my solution for Step 4?

You could measure execution time. Depending what you are after. You can google how to do that

ah, step 2 of instruction step 8.

Well, we have punctuation in the sentence, which `split()` doesn’t get rid of, so we would need a regular expression or something:

``````// should we convert to lowercase? Given string comparison is case sensitive?
story = story.toLowerCase();
var re = /['a-z]+/gi;

var storyWords = story.match(re);
``````

`reduce()` seems to be a good option, we could do it manually of course:

``````const d = {}
const storyLength = storyWords.length;
for (let i = 0; i < storyLength; i++){
let x = storyWords[i]
d[x] = ++d[x] || 1
}

console.log(d);
``````

undefined + 1 gives NaN, which is false

Now that we have the dictionary, we can use a loop to determine the most occurring word.

just make two variable:

``````let mostCommonWord = null; // or undefined?
let numberOfOccurences = 0;
``````

then loop over the dictionary, if any word has higher value just update those two variables

once you got a solution, you can optimize from there

``````let overusedCounter = 0;
for (let betterWordsIndex = 0; betterWordsIndex < betterWords.length; betterWordsIndex++) {
for (let overusedWordsIndex = 0; overusedWordsIndex < overusedWords.length; overusedWordsIndex++){
if (betterWords[betterWordsIndex] === overusedWords[overusedWordsIndex]) {
overusedCounter += 1;
}
}
}

console.log('There are ' + overusedCounter + ' overused words.');
``````

Hi. Thats what i actually did on step 4. Dont get the problem with step 8.1, u could iterate array the same way u did in step 3. Actually I rly have no idea what to do in step 8.2. I guess i could just create 179 arrays with each word in it and iterate every single of it through the text, but that sounds like im going crazy right now.

Well, you kinda do. Your current code dumps all wordCounts into one (overusedWordsCount). But you were asked to tell how many times each word was counted. You didn’t have to do it the way the instructions told you because you didn’t achieve the instructor’s goal.