I need help with Loops in Javascript Separating the Rotten Strawberries

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

//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 = 1; i <= 500; i++); i < goodStrawberries; i++;
if(i % 5 === 0); {
goodStrawberries–;
}
Oops, try again. Your value for goodStrawberries is incorrect. Check your arithmetic and your loop bounds.

```

Replace this line with your code.

<do not remove the three backticks above>

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

this line:

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

why is there a semi-colon? It interrupts the if clause, so the code between the curly brackets doesn’t work properly

3 Likes

I removed it still doesnt work :c

you for loop:

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

can you explain the logic?

3 Likes

//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 = 1; i <= 500; i++); i < goodStrawberries; i++;
if(i % 5 === 0) {
goodStrawberries–;
}

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

looking at the docs:

we are provided with the general syntax:

for ([initialization]; [condition]; [final-expression])
   statement

and then how this general syntax translates into an actual loop:

for (var i = 0; i < 9; i++) {
   console.log(i);
   // more statements
}

does your loop look similair? Not really, use the docs to try and fix the loop, let me know if you need more help

4 Likes

Thank you I got it now :slight_smile:

1 Like

What was it? I’m stuck on the same problem and can’t figure it out.

the loop wasn’t good

If you still need help, make a new topic (see guidelines), you shouldn’t hijack an existing topic.

ensure you provide all the details needed to make a good topic, so we can help you effectively

this include:

your code
error message
exercise url

this will also be asked for by the template which you get when creating a topic, otherwise see here:

on tips to write good topics and posts

2 Likes