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 McKibbin
Eric McKibbin
11,458 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