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 trialdanielsterbro
Full Stack JavaScript Techdegree Graduate 25,529 PointsWrite 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?
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
Alexander Davison
65,469 PointsWow, 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! 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
andren
28,558 PointsThe 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
21,058 Pointsnamespace Treehouse.CodeChallenges
{
class Frog
{
public readonly int TongueLength;
public Frog(int tongueLength)
{
TongueLength = tongueLength;
}
bool check = TongueLength >= distanceToFly;
return check;
}
}
}
Andrew Day
1,389 Pointsthis 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;
}
}
}
Derrick Threatt
538 PointsI 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?
Jia Tian Xia
4,035 PointsYou have two white space after EatFly( before int and after distanceToFly that doesn't make it work
Scott George
19,060 PointsSimilar 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;
}
}
}
danielsterbro
Full Stack JavaScript Techdegree Graduate 25,529 Pointsdanielsterbro
Full Stack JavaScript Techdegree Graduate 25,529 PointsThank you.
Abubakar Gataev
2,226 PointsAbubakar Gataev
2,226 PointsHow did you know that you had to use with a capital T and why not tongueLength.