Another Mini Linter

I’m having issues with a piece of code that I’ve copied below. When I run the code, I am getting a number count for each of the unnecessaryWord variables that is three times higher than it should be. The code is functioning as intended, but when it comes to a word that is included in the unnecessaryWords array, it counts three times and I’m not sure why. I realize I could set it so (betterWords[i] === ‘very’) or one of the other two, and it would probably fix it, but I’m really trying to tie the comparison to the unnecessaryWords array.

Any advice would be appreciated.

Edit: I tried using the literal array value that I’m trying to count and it still counts it three times so I really don’t know what’s wrong now.

Edit2: I figured it out. My second for loop was unnecessary with the else/if statements and was causing it to loop.

1 Like

Great that you discovered the issues. It would be interesting to see how you’ve resolved this. Please share a new gist.

Aside

Consider,

if A == false

then,

not A == true

thus,

if ( ! unnecessaryWords.includes(word) )

Such an expression lets us leverage truthiness and not make direct comparisons to booleans. It’s a different twist on the original logic that alters nothing. It reads toward truthy, as opposed to falsy.

Minor constructs such as this can benefit from a “pillow to pound weight comparison”. Which is easier to understand? The more positively definitive, one would venture.

1 Like

It kind of clicked in my head as soon as I asked for help with it. I banged it around in my head all day and couldn’t figure it out, but as soon as I started explaining the problem it seemed really obvious what the problem was, even though I’m still not entirely sure exactly why it was causing the problem.

“Such an expression lets us leverage truthiness and not make direct comparisons to booleans. It’s a different twist on the original logic that alters nothing. It reads toward truthy, as opposed to falsy”

I understand what you’re saying here, and I think I used .includes in the proper way further down in the code, but at the time I was writing it, making unnecessaryWords.includes… === false seemed to make the most sense. So, you’re saying that I should not be making .includes directly === to true or false in the best usage practice?

“Minor constructs such as this can benefit from a “pillow to pound weight comparison”. Which is easier to understand? The more positively definitive, one would venture.”

I’m not sure what you mean by this.

1 Like

I don’t know that it relates to best practice, but it is simpler. Think truthy, not true, and falsy, not false. We never need to make a literal comparison to a boolean, just test the expression (a value) for truthiness.

Only a truthy expression or value will make into the first branch of an if statement or into the loop of a while statement. We don’t need to see the actual boolean, only the outcome of the evaluation by which branch is followed.

“Pillow to pound weight comparison” is a made up expression of mine that is essentially a comparison of complex to simple, code heavy to code light. If something looks messy (pillow), then it can be made to look less messy (pound weight).

1 Like