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 Loops and Final Touches For Loops

Kiem Ruach
Kiem Ruach
2,267 Points

C# challenge

im really have no luck

FrogStats.cs
namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        i = 0;
        Frog[] frogs = new Frog[i];
        public static double GetAverageTongueLength(Frog[] frogs)
        {
        for(i = 0; i < frogs.Length i++)
            {
            return frogs/frogs.Length;
            }
        }
    }
}
Frog.cs
namespace Treehouse.CodeChallenges
{
    public class Frog
    {
        public int TongueLength { get; }

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

1 Answer

Steven Parker
Steven Parker
231,268 Points

You're getting close, here's a few hints:

  • all the code should go inside the method
  • you won't need to create a new frog instance
  • you won't want to return until after the loop finishes
  • since "frogs" is an array, you can't do math on it directly
  • you might use the loop to build up a sum of tongue lengths of all the frogs
  • after the loop, dividing the sum by the number of items in the array will give you an average