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 Methods

I don't understand

I think there may be something off about the way the treehouse software checks my code.

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

        public Frog(int tongueLength)
        {
            TongueLength = tongueLength;
        }

        public bool EatFly(int distanceToFly, int TongueLength)
        {
            bool reach;
            if ( distanceToFly == TongueLength)
            {
                reach = true;
            }
            else if (distanceToFly != TongueLength)
            {
                reach = false;
            }


            return reach;
        }
    }
}

1 Answer

Eric M
Eric M
11,545 Points

Hi Travis,

Sometimes the suggestions don't give the exact issue with your code. I like to check things in another editor, or try to compile locally, if I'm stuck.

In the code sample you've posted there are a couple of things we can change.

Firstly you're only checking if if the distance to the fly is the exact length of the tongue. What if the fly is closer to the frog than the tongue length? It should still be able to eat the fly. That condition should be less than or equal to (<=) instead of exactly equals (==).

Once we've got that we don't need an else if clause, we can just use else as there are no further conditions to check.

Finally TongueLength is a property of Frog that gets initalised. We don't need to take it as a parameter to this method, we can just reference it directly, so you can remove the int TongueLength parameter from your method signature (the test cases won't pass it in).

Cheers,

Eric