Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Objects Methods Method Overloading

Keep Getting Error About Reaction Time

I need some help, I have confidence that the code is okay- but I keep receving the error "If the frog's reaction time is greater than the fly's reaction time, the frog can't get the fly."

Am I missing something?

Frog.cs
namespace Treehouse.CodeChallenges
{
    class Frog
    {
        public readonly int TongueLength;
        public readonly int ReactionTime;

        public Frog(int tongueLength, int reactionTime)
        {
            TongueLength = tongueLength;
            ReactionTime = reactionTime;
        }

        public bool EatFly(int distanceToFly)
        {
            return TongueLength >= distanceToFly;
        }

        public bool EatFly(int distanceToFly, int flyReactionTime)
        {
            if(TongueLength >= distanceToFly && ReactionTime <= flyReactionTime);
            {
                return true;
            }
        }
    }
}

1 Answer

Steven Parker
Steven Parker
231,275 Points

There's a stray semicolon after the "if" statement that prevents it from working, and the function still needs to return a value when the condition is not true.

When I remove the ; at the end of the if statement I get

Frog.cs(19,21): error CS0161: `Treehouse.CodeChallenges.Frog.EatFly(int, int)': not all code paths return a value
Compilation failed: 1 error(s), 0 warnings

I'm still a little confused; do I omit the semicolon? And from what you said:

and the function still needs to return a value when the condition is not true.

Should I create an else statement? :( I appreciate the help!

Steven Parker
Steven Parker
231,275 Points

You don't need an "else" since the function will have already returned otherwise.
But you do need a "return false;" at the end to fulfill the requirement of returning a bool.

Oh! Got it! Now I know what needs some practice. Thank you! I marked this as the best answer.