Mini Linter step 5 help please!

Hi everyone, I am trying to use for loop instead of forEach method to count how many sentences are in the paragraph but I think I did not do it correctly, I tried for hours but I could not covert forEach to for Loop correctly.
Please help!
https://www.codecademy.com/courses/introduction-to-javascript/projects/mini-linter

It looks like they want you to use a forEach loop specifically and not a for-loop (but I don’t have access to pro so I can’t verify).

The instruction simply suggests iterating over the array of words, it doesn’t proscribe a way to do it. :slight_smile:

The logic that you’ve implemented in your for loop is completely wrong, tbh. I think this is because you’ve looked at the hint, not understood what it is doing, and have tried to cobble together something approximating it inside the for loop.

If you can’t explain verbally the process you’re attempting to implement in code, you probably can’t code it. I’d suggest you write out the steps necessary, without involving any code at all, to accomplish the task at hand:

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.
2 Likes

I tried this but still not correct :frowning: The output is 0.

let sentenceCount = 0;
for ( let i=0; i < storyWords.length; i++) {
if (("!" || “.” ).includes(storyWords.length-1[i])) {
sentenceCount++
}
}
console.log(sentenceCount);

Yes, because the logic is still wrong.

You’ve decided on using a for loop, which is fine. You need to work out precisely what you need to do inside that loop.

Explain to me - without using any code - precisely what steps you need to take inside the for loop to achieve the outcome requested in the task. If you’re stuck, look at it this way:

I’ve given you a printed copy of that story, and asked you to count how many sentences there are in it. A sentence ends with a . or an !. How would you count the sentences?

Once you know how you would solve the problem, as a human, you can begin to adapt those steps to instructions that the computer can do for you.


To me, at the moment it looks like you’re making wild guesses about how to maybe adapt the code from the hint to suit your desired approach. I don’t think you understand what that code is doing, or how to solve the problem, and so you’re throwing random functionality at the problem in the hope that you’ll stumble upon something which does what you want.

This is not a good approach.

None of this is intended to be harsh, though it may seem that way, and I don’t mean to discourage you. However, programming is the art of giving a computer instructions to complete a task and if you yourself do not understand how to complete that task then you cannot tell the computer how to do it.

Devise the process first, then implement that process in code.

1 Like

@thepitycoder
Wow I did it, please check if this method is correct, the output is 12 which is correct.
If it’s correct I would still love to know how you would have done it.
And I wanted to instead of having 2 if conditions I wanted to put them together with && but I did not manage.
Any suggestion, consideration or critic is highly appreciated, because this is how we learn!
Thanks a lot for your time!

let sentence = 0;

for ( let i = 0; i < storyWords.length; i++) {
if (storyWords[i].includes("!")) {
sentence++
}
if (storyWords[i].includes("."))
sentence++
}
console.log(sentence++)

I tried to put them together in 2 different ways:
FIRST:

if (storyWords[i].includes("!" && “.”)) {
sentence++
}

SECOND:

if (storyWords[i].includes("!") && storyWords[i].includes(".")) {
sentence++
}

///BUT DID NOT WORK :confused:

Hi @thepitycoder, could you please check my code and give me a feedbck :slight_smile:
Thanks a lot!