I need help with Loops in Javascript Separating the Rotten Strawberries


#1

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

#2

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


#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


#4

I removed it still doesnt work :c


#5

you for loop:

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

can you explain the logic?


#6

//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.


#7

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


#8

Thank you I got it now :slight_smile:


#9

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


#10

#11

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