Stawberries arithmetic


#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.>
the arithmetic is incorrect. I have tried % and - 1 doesn’t seem to work.

```

var goodStrawberries = 500;

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

<do not remove the three backticks above>

#2

This will result in an infinite loop. i should be incremented instead of decremented. (i++). The initial value should be 1 to match the first index (1…500).

Your code should be decrementing goodStrawberries inside the loop, which means is cannot be used in the conditional else the loop will end prematurely and the result will be wrong.


#3

This shouldn’t be difficult to solve. deduct 1 from every 5 goodStrawberries. Any idea would help?

var goodStrawberries = 500;

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


#4

That is not what the loop should be doing. It should be subtracting 1 from goodStrawberries if the index is divisible by 5. You will not get the correct result if you use that variable in the conditional. Your answer will be 416 when it should be 400. What might you do to rectify that?


#5

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