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 add1
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