Mini Linter Step 5 - Using endsWith() method

Hello fellow coders,

In step 5 of the Mini Linter project, the proposed solution uses word.length-1 to target the final character of the string elements in the storyWords array, but when I was trying to figure the solution on my own, I found the endsWith() method and decided to try that. I don’t get any error but the sentences counter variable stays at 0 when I log it to the console.

The instructions are as follows:

Now, count how many sentences are in the paragraph.

This may seem tricky, but remember that all of the sentences in this paragraph end with a period ( . ) or an exclamation mark ( ! ). You could iterate over the array and add 1 to a sentence counter variable for each word that has a period or exclamation mark as its final character.

The solution code from Codecademy is:

let sentences = 0;

storyWords.forEach (word => {
  if (word[word.length-1] === '.' || word[word.length-1] === '!') {
    sentences++;
  }
})

const logCount = (count1, count2, count3) => {
  console.log(`This is the number of words in this paragraph: ${count1}`);
  console.log(`This is the number of sentences in this paragraph: ${count2}`);
  console.log(`This is the number of times each overused word appears in this paragraph: ${count3}`);
};

logCount(storyWords.length, sentences, overusedWordsTotal); // Returns 12 sentences

This works fine when I run it.

Now my original code:

let sentences = 0;

storyWords.forEach(word => {
  if (word.endsWith('.', '!')) {
    sentences++;
  }
});

const logCount = (count1, count2, count3) => {
  console.log(`This is the number of words in this paragraph: ${count1}`);
  console.log(`This is the number of sentences in this paragraph: ${count2}`);
  console.log(`This is the number of times each overused word appears in this paragraph: ${count3}`);
};

logCount(storyWords.length, sentences, overusedWordsTotal); // Returns 0 sentences

For the life of me I can’t figure out why it doesn’t add 1 to the sentences variable while the structure of Codecademy’s solution and mine seem similar.

If anyone can hint me towards what I’m missing, I’m really itching to understand :slight_smile:

Kudos on looking up a method to accomplish a specific task! You may want to re-read the documentation, however. Can you provide a comma separated list of values as the argument to endsWith()? What happens if you try your code with only '.' or '!' as the argument? Consider how the Codecademy solution differs from yours.

1 Like

consider using reduce, you’d set the accumulating value to initial 0 and add 1 when the condition is met and otherwise pass along the original

it’d be mostly the same, with the difference that your expression would have a result rather than a side-effect

const sentences = storyWords.reduce(
    ...
)

alternatively you could use filter and length

Dear midlindner, thank you very much for your quick response. You’re absolutely right, I overlooked the details in the documentation :slight_smile:
When I saw the exemples in MDN, I wrongly assumed the number was another value in the string (hence why I thought the method could take multiple values separated by a coma as arguments) but it actually refers to the length of the string. :face_with_monocle:

Rewrote the code like so and now it works!

let sentences = 0;

storyWords.forEach(word => {
  if (word.endsWith('.') || word.endsWith('!')) {
    sentences++;
  }
});

So in the end it’s still a bit lengthy, but I find it at least a lot more understandable to remember what it does when using that method over the word[word.length-1]!

Thanks again for the help!

1 Like

Thank you for the extra tips! I’m going to call it a night, but it’s very interesting to see the array ( :v:) of solutions one can come up with to achieve the same thing!