Issue with Strawberry if statement


#1

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

I am having trouble getting through the Strawberries separation problem. This part of the exercise is asking for this:

You’ve worked hard to pick 500 strawberries, but it looks like every 5th
strawberry you picked was a rotten one! If each strawberry has an index
value, write an if statement that returns true if the index is
divisible by 5.

It’s not asking for the “for” loop just yet. All it is asking for is a simple “if” statement. Below is the code that I have submitted:

//Here is an example of a strawberry index
var index = 20;

//Boolean value rotten
var rotten = false;

//If the index is divisible by five, the strawberry is rotten, so set
//rotten to true. False, otherwise
if (index % 5 === 0) {
rotten = true;
return rotten;
} else {
return rotten;
}

Am I missing something? I keep getting this syntax error:

SyntaxError: return not in function

I have return statements in the if statement, have I done something else wrong? I have tried not changing “rotten” to “true” and instead have just returned true, same error.


#2

return can only be used in function, you don’t have functions, so you can’t use return

if index is divisible by 5 set rotten to true, else set rotten to false, rotten has to be set to false in the else clause


#3

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