Mini-Linter project, but my real question is conceptual in nature

Over the last weeks, I worked my way through the projects, sometimes with instant succes, sometimes after several tries. And while I think I understand my code after it is complete, I still have the sense that I know very little of what I am supposed to do when I start a new line of code. Case in point; the code below is from the Mini-Linter assignment.

const storyWords = story.split(' '); 
const howManyWords = storyWords.filter(word => overusedWords.includes(word) 
const howManySentences = storyWords.filter(word => word.endsWith('.');

Not all of the code is there, but what I have gets me past step 4 and working on step 5. The final line of code shows that I count periods, but not exclamation marks, yet. But my real question is this. The two final lines of code are very similar. Both create a variable, then to ‘fill’ it, refer to the same array storyWords and apply the same method. Then, they take an element and call it word when in between brackets, but then there is a difference. Both lines apply a different method on word, but one should only work on arrays, and the other on strings. How is this possible? How can word be both an array and a string? Is this some kind of flexibility that JavaScript offers?

Both lines don’t apply different method on word.

On the second line => overusedWords.includes(word) you are applying includes() method on overusedWords and passing word as an argument.

Thanks, I didn’t know why I didn’t look at it liek that. But I’m not there yet. My issue is with how I created word in the second and third line of code. As I understand it, I created it as an argument in an implicit function, as a result of the filter method. Either way, both lines ‘create’ word in the same way, so it should have either been an array element or a string.

But whatever it is, word is at least an argument, that seems certain, right?

Word count does not require any special methods (the above is not counting anything). You’ve split the string into an array, now simply poll its length.

Thanks for all the input. I’ll try to put it to use. But I think I’ll be on the ‘don’t know how I made that work’ path for a while.

1 Like