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 trialPulla Rao Charugalla
Courses Plus Student 2,755 PointsI've been stuck for days with a problem i have no idea how to solve
Please fix the problem in the attached file as soon as possible. I could not proceed
namespace Treehouse.CodeChallenges
{
class Frog
{
public readonly int TongueLength;
public readonly int ReactionTime;
public Frog(int tongueLength, int reactionTime)
{
TongueLength = tongueLength;
ReactionTime = reactionTime;
}
public bool EatFly(int distanceToFly)
{
return TongueLength >= distanceToFly;
}
public bool EatFly(int distanceToFly, int flyReactionTime)
{
// return true;
if ((TongueLength >= distanceToFly) && (ReactionTime >= flyReactionTime))
return true;
else return false;
}
}
}
2 Answers
gyorgyandorka
13,811 PointsI've tried your code, and it gives a hint actually: "Bummer! If the frog's reaction time is greater than the fly's reaction time, the frog can't get the fly." If the frog has a faster reaction time, it means it is less than the fly's.
By the way, in such situations you can simply return the boolean expression itself (since it evaluates to true
or false
anyway), no need to write out the if-else blocks:
return ((TongueLength >= distanceToFly) && (ReactionTime <= flyReactionTime));
James Churchill
Treehouse TeacherPulla,
Were you able to solve the code challenge after Gyorgy's suggestion? If not, how can we further help?
Thanks ~James
Steven Parker
231,198 PointsSteven Parker
231,198 PointsAnother reason to not wrap the returns in if...else is that the compiler might otherwise think you failed to return a value in all code paths.
gyorgyandorka
13,811 Pointsgyorgyandorka
13,811 Points"The compiler might otherwise think you failed to return a value in all code paths" - could you elaborate, Steven? I don't really understand what you mean by this.
Steven Parker
231,198 PointsSteven Parker
231,198 PointsThe compiler might not understand that the conditional code covers all possible cases. So it might give you a compile error. Try it out and see.
gyorgyandorka
13,811 Pointsgyorgyandorka
13,811 PointsBut if there is an
else
branch (with a return statement) then all possible cases must have been covered (by definition), mustn't they?Steven Parker
231,198 PointsSteven Parker
231,198 PointsLogically, yes. But would the compiler still give an error? Try it out and see.