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 trialTravis John Villanueva
5,052 Pointsreturning a value from a for each loop
Can you correct my code and i will digest it from there?
namespace Treehouse.CodeChallenges
{
class FrogStats
{
public static double GetAverageTongueLength(Frog[] frogs)
{
foreach(Frog frog in frogs)
{
return frog.TongueLength;
}
}
}
}
namespace Treehouse.CodeChallenges
{
public class Frog
{
public int TongueLength { get; }
public Frog(int tongueLength)
{
TongueLength = tongueLength;
}
}
}
1 Answer
Steven Parker
231,275 PointsYou might have a better learning experience if you fix it yourself.
Here's a few hints that may help:
- don't return inside the loop, that means the loop can never go past the first item
- you probably want to declare a variable to collect a total length before the loop
- inside the loop, you might want to add the frog's length to the total
- after the loop, if you divide that total by the number of frogs you could get an average
- then you could return that average