Separating good strawberries


#1

<PLEASE USE THIS TEMPLATE TO HELP YOU CREATE A GREAT POST!>
https://www.codecademy.com/en/courses/introduction-to-javascript/0/3

The console says my result of the exercise is not correct, and I don’t find the mistkae.



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


What am I doing wrong?

Thanks.


#2

You are beginning your for loop at index 0, where you should be beginning it at index 1.


#3

I did that and it still returned a false number.


#4

Change if(i%5===0) to if(i%4===0)

and you’re good to go


#5

How come? I haven’t tested it yet but I don’t understand why using 4 instead of 5.

EDIT: It worked, still idk why.


#6

Hi, you could also start with var i=1, and use i%5

for (var i = 1; i <= 500 ; i++) {
if (i % 5 === 0 ) {
goodStrawberries = – goodStrawberries ;
}
}
console.log (goodStrawberries)


#7

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