Rotten strawberries


#1

Hi there,

I am new in JS and I do not quite understand this exercise. Could someone tell what exactly I do have to do here?

Thanks a million.

<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/en/courses/introduction-to-javascript/0/2#

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>

//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 (20 % 5) {
return true;
}
else {
return false;
}

Replace this line with your code.

#2
if (20 % 5)

will be true when 20 is NOT divisible by 5. Switch your return statements around and your if statement will work.

Otherwise, leave the returns as is, and change the conditional…

if (20 % 5 === 0)

#3

I tried switching the return statements:

if (20 % 5) {
return false;
}
else {
return true;
}

as well as

if (20 % ===0) {
return true;
}
else {
return false;
}

Either way, I get the following error message: SyntaxError: Illegal return statement

Is there something I’m missing?


#4

return is to be used within a function AFAIK.

Your exercise wants you to do something else.

They have provided you with two variables; index of strawberry and the variable rotten.

You need to check if the index is divisible by 5 using the modulus operator. Check if it returns 0 as @mtf stated.

If it does, set the rotten variable to true.

Let me know if that helps!


#5

D’oh! I didn’t check the lesson and just assumed the code was in a function. My bad.

It should be rotten = false or rotten = true.

Also,

We should be using the variable index in the conditional…

if (index % 5) {
    rotten = false;
} else {
    rotten = true;
}

or,

if (index % 5 === 0) {
    rotten = true;
} else {
    rotten = false;
}

#6

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