Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
218,596 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
218,596 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.