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 trialTrailhead Boise5
4,979 Points"Did you create a method named EatFly that returns a bool value?" error.
I keep running into this error that says I don't have an EatFly method that returns a bool value. Here's my code for the error.
namespace Treehouse.CodeChallenges { class Frog { public readonly int TongueLength;
public Frog(int tongueLength)
{
TongueLength = tongueLength;
}
public Eatfly(int distanceToFly)
{
bool eaten = distanceToFly = true;
bool eaten != distanceToFly != true;
return eaten;
}
}
}
I have an EatFly method, but I'm guessing its not returning any bool values. Any ideas?
2 Answers
Steven Parker
231,269 PointsYou need to declare the return type between public and EatFly.
The definition of Frog above is not a good example to follow because it is a constructor, and constructors do not have return types.
Also, inside the method, you should only declare "eaten" one time. And what you assign to it should be the result of comparing the distance with the tongue length.
ujz ujz
Courses Plus Student 6,664 PointsThe frog can only eat if its tongue is as long as the distance. And that's what why this method Eatfly exists: to check if the frog can reach or not.
So in your code, you need to check if it can reach or not. Here's what I wrote and it passed the checker:
public bool Eatfly (int distanceToFly)
{
//this returns true only if the tongue is long enough
bool eaten = TongueLength >= distanceToFly;
return eaten;
}
One thing I personally did not understand was why we need the return eaten part. Doesn't this method automatically return the true or false after it finishes running?
Andrew Perry
5,266 Pointsit doesn't automatically return anything unless you use the return keyword. You could put instead
return TongueLength >= distanceToFly;
Trailhead Boise5
4,979 PointsTrailhead Boise5
4,979 PointsHere's my new code based on your feedback.
namespace Treehouse.CodeChallenges { class Frog { public readonly int TongueLength;
} }
I'm still running into the same error. I don't quite understand what you meant about comparing the distance with the tongue length. Can you elaborate a little more?