Mini Linter question

Hi all!

I’m working through Mini Linter and came across something I didn’t quite understand.


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’ ];

let storyWords = story.split(’ '); // Step 1
console.log(storyWords.length); // Step 2

The next step is to filter out unnecessaryWords from storyWords and return a new array to betterWords. I tried two ways below and I’m wondering why the output is different:

let betterWords = storyWords.filter(words => {
if (words != unnecessaryWords) {
return words
console.log(betterWords.length); // output is 185

let betterWords = storyWords.filter(words => {
return !unnecessaryWords.includes(words);
console.log(betterWords.length); // output is 182, which I believe is correct.

Would someone be able to tell me why these are producing different outputs?

Thank you!

The filter method takes a predicate function. The return of a predicate function is a boolean. The second example above has such a callback, where the first example returns a non-boolean.


Just putting this out there…

Choice of variable name often involves a singularity or plurality consideration. The code above uses a pluralized variable name, words which is misleading. Only one word at a time is iterated. Sure, we’re iterating words, so it’s easy to see why we would choose that name, but I would go with,

word =>

so the includes method call looks like,


Think how accurately it reads for an individual word. If we pluralize, it reads more like a blanket statement applying to ALL words. Granted, we can say it’s just semantics. It bears consideration given there will be other human readers of our code.

Given that a predicate function returns a boolean, it follows that we would not write a conditional into the function. It is already an assertion.

____.filter(x => +x)

The above asserts that x is, or can be cast to a Number.

 > a = "abcxyz".split("").join("1").split("")
<- > (11) ["a", "1", "b", "1", "c", "1", "x", "1", "y", "1", "z"]
 > a.filter(x => +x)
<- > (5) ["1", "1", "1", "1", "1"]

No it doesn’t
It’ll make the coercion to Number, and then test whether the result is truthy
There are non-numbers that’ll get coerced to truthy values and vice versa

> typeof a
> Boolean(+a)

> typeof b
> Boolean(+b)
1 Like
 > +true
<- 1

I get it; d’oh.

That particular instance is quite reasonable, but you can also get things like:

> typeof a
> +a

Here, the value of a is [348], the value for b was 0

So, useful as it is to implement the unary plus operator, there are enough hurdles to jump that preclude its use, at least on wholesale terms.

In the example we supplied the string. The rest went from there. We knew what we were extracting… Exactly what we inserted in the first place. The environment was sufficiently ensconced bearing appropriate types.

Is it?
What would you even argue that it does? Is it a meaningful operation?

It does what I wanted it to do, in a controlled environment.

Thanks @mtf. And I appreciate you flagging the plurality of my variable name. I hadn’t thought that of that :slight_smile:

1 Like

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.