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

Write a method inside the Frog class named EatFly.

Question Write a method inside the Frog class named EatFly. It should take a single integer parameter named distanceToFly and return a bool value. It should return true if the frog can reach the fly with its tongue, and false otherwise. Whats wrong with my code?

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

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

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

6 Answers

Wow, I can't belive how long it took for me to find the problem in this code. It took around 10 minutes. But I finally found the error! :smile: Let's say tongueLength = 10 and distanceToFly = 5. Your code was:

bool check = TongueLength <= distanceToFly

Which, in fact, is really nice code and this is probably the cleanest, nicest code I've seen in the Community this week. But, there's one problem (which took ten minutes to find). You are basically saying, still keeping our values of tongueLength = 10 and distanceToFly = 5, this is saying "is 10 less than or equal to 5"? Which, of course, is false because 10 is actually greater than 5. So, to fix that, you should say "distanceToFly <= TongueLength". This is our code that's nice and fixed:

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

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

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

Hope this helps! ~Alex

Thank you.

How did you know that you had to use with a capital T and why not tongueLength.

andren
andren
28,558 Points

The logic is wrong, you are returning true if the frog's tongue has a length less or equal (<=) to the distance to the fly. You are supposed to return true if the frog's tongue length is greater or equal (>=) to the distance to the fly.

Also while it's not strictly wrong, creating a variable to hold the boolean and then immediately returning that variable is not actually necessary. You can just return the result of the evaluation directly like this:

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

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

        public bool EatFly(int distanceToFly)
        {
            return TongueLength >= distanceToFly;
        }
    }
}
HIDAYATULLAH ARGHANDABI
HIDAYATULLAH ARGHANDABI
21,058 Points
namespace Treehouse.CodeChallenges
{
    class Frog
    {
        public readonly int TongueLength;

        public Frog(int tongueLength)
        {
            TongueLength = tongueLength;
        }
 bool check = TongueLength >= distanceToFly;
            return check;
  }
    }
}
Andrew Day
Andrew Day
1,389 Points

this worked for me

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

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

        public bool EatFly(int distanceToFly)
        {
            bool check = distanceToFly < TongueLength;
            return check;

        }
    }
}

I had the exact same answer and it didnt work for me.

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

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

        public bool EatFly( int distanceToFly )
        {

            bool check = distanceToFly < TongueLength;
            return check;

        }
    }
}

I got : Bummer: Did you create a parameter in your method named distanceToFly?

You have two white space after EatFly( before int and after distanceToFly that doesn't make it work

Similar to what has been posted, but worded a bit differently. It worked for me. namespace Treehouse.CodeChallenges { class Frog { public readonly int TongueLength;

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

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

       return isTrue; 
    }
}

}