Loops in Javascript - Separating the Rotten Strawberries


#1

<PLEASE USE THIS TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>

https://www.codecademy.com/courses/introduction-to-javascript/0/3#

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>

Error Message: Oops, try again. Your value for goodStrawberries is incorrect. Check your arithmetic and your loop bounds.

```

//You have 500 good strawberries, indexed from 1 to 500
var goodStrawberries = 500;

//For every strawberry whose index is divisible
//by 5, decrement the value of goodStrawberries by 1
for (var i = 0; i < goodStrawberries; i++) {
if(i % 5 === 0); {
goodStrawberries–;
}
}

console.log(goodStrawberries);

<do not remove the three backticks above>


HELP - I am stuck and the one article on here regarding this has code that doesn't work!  What happened to the helpful hint section of Codecademy???

Thanks!

#2

Hi @bellatrixls, because you have 500 good strawberries, indexed from 1 to 500, you would want to count from 1 and include the 500th strawberry as well.


So,

for (var i = 1; i <= 500; i++) {

Do not replace the 500 with goodStrawberries because of how it interacts with the goodStrawberries-- later, you will not get 400 as a result.

You can try it and see for yourself.


One more thing:
Remember the placement of semicolon, it is important.

Here your code:

if(i % 5 === 0); {

That semicolon terminates the IF statement, causing the rest of block code not being executed. Do remove it, only then the IF statement can be executed.


After changing your code, the expected result and logical mathematical answer is 400. If you get any other number, means your code still do not work.

Hope it helps. :slight_smile:


#3

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